]>
Commit | Line | Data |
---|---|---|
dc05df57 | 1 | /* Target-vector operations for controlling windows child processes, for GDB. |
0a65a603 | 2 | |
b811d2c2 | 3 | Copyright (C) 1995-2020 Free Software Foundation, Inc. |
0a65a603 | 4 | |
e6433c28 | 5 | Contributed by Cygnus Solutions, A Red Hat Company. |
e88c49c3 | 6 | |
24e60978 SC |
7 | This file is part of GDB. |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 11 | the Free Software Foundation; either version 3 of the License, or |
24e60978 SC |
12 | (at your option) any later version. |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
a9762ec7 | 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
24e60978 SC |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
24e60978 | 21 | |
dfe7f3ac | 22 | /* Originally by Steve Chamberlain, sac@cygnus.com */ |
24e60978 SC |
23 | |
24 | #include "defs.h" | |
25 | #include "frame.h" /* required by inferior.h */ | |
26 | #include "inferior.h" | |
45741a9c | 27 | #include "infrun.h" |
24e60978 | 28 | #include "target.h" |
24e60978 SC |
29 | #include "gdbcore.h" |
30 | #include "command.h" | |
fa58ee11 | 31 | #include "completer.h" |
4e052eda | 32 | #include "regcache.h" |
2a3d5645 | 33 | #include "top.h" |
403d9909 CF |
34 | #include <signal.h> |
35 | #include <sys/types.h> | |
36 | #include <fcntl.h> | |
403d9909 CF |
37 | #include <windows.h> |
38 | #include <imagehlp.h> | |
2b008701 | 39 | #include <psapi.h> |
10325bc5 | 40 | #ifdef __CYGWIN__ |
b7ff339d | 41 | #include <wchar.h> |
403d9909 | 42 | #include <sys/cygwin.h> |
b7ff339d | 43 | #include <cygwin/version.h> |
10325bc5 | 44 | #endif |
a1b85d28 | 45 | #include <algorithm> |
93366324 | 46 | #include <vector> |
cad9cd60 | 47 | |
0ba1096a | 48 | #include "filenames.h" |
1ef980b9 SC |
49 | #include "symfile.h" |
50 | #include "objfiles.h" | |
92107356 | 51 | #include "gdb_bfd.h" |
de1b3c3d | 52 | #include "gdb_obstack.h" |
fdfa3315 | 53 | #include "gdbthread.h" |
24e60978 | 54 | #include "gdbcmd.h" |
1e37c281 | 55 | #include <unistd.h> |
4646aa9d | 56 | #include "exec.h" |
3ee6f623 | 57 | #include "solist.h" |
3cb8e7f6 | 58 | #include "solib.h" |
de1b3c3d | 59 | #include "xml-support.h" |
463888ab | 60 | #include "inttypes.h" |
24e60978 | 61 | |
6c7de422 MK |
62 | #include "i386-tdep.h" |
63 | #include "i387-tdep.h" | |
64 | ||
31b060a2 CF |
65 | #include "windows-tdep.h" |
66 | #include "windows-nat.h" | |
df7e5265 | 67 | #include "x86-nat.h" |
ecc13e53 | 68 | #include "complaints.h" |
51a9c8c5 | 69 | #include "inf-child.h" |
268a13a5 TT |
70 | #include "gdbsupport/gdb_tilde_expand.h" |
71 | #include "gdbsupport/pathstuff.h" | |
559e7e50 | 72 | #include "gdbsupport/gdb_wait.h" |
ae1f8880 | 73 | #include "nat/windows-nat.h" |
de1b3c3d | 74 | |
46f9f931 HD |
75 | #define STATUS_WX86_BREAKPOINT 0x4000001F |
76 | #define STATUS_WX86_SINGLE_STEP 0x4000001E | |
77 | ||
4834dad0 TT |
78 | using namespace windows_nat; |
79 | ||
418c6cb3 | 80 | #define AdjustTokenPrivileges dyn_AdjustTokenPrivileges |
2b008701 CF |
81 | #define DebugActiveProcessStop dyn_DebugActiveProcessStop |
82 | #define DebugBreakProcess dyn_DebugBreakProcess | |
83 | #define DebugSetProcessKillOnExit dyn_DebugSetProcessKillOnExit | |
84 | #define EnumProcessModules dyn_EnumProcessModules | |
46f9f931 | 85 | #define EnumProcessModulesEx dyn_EnumProcessModulesEx |
2b008701 | 86 | #define GetModuleInformation dyn_GetModuleInformation |
418c6cb3 CF |
87 | #define LookupPrivilegeValueA dyn_LookupPrivilegeValueA |
88 | #define OpenProcessToken dyn_OpenProcessToken | |
cd44747c PM |
89 | #define GetConsoleFontSize dyn_GetConsoleFontSize |
90 | #define GetCurrentConsoleFont dyn_GetCurrentConsoleFont | |
46f9f931 HD |
91 | #define Wow64SuspendThread dyn_Wow64SuspendThread |
92 | #define Wow64GetThreadContext dyn_Wow64GetThreadContext | |
93 | #define Wow64SetThreadContext dyn_Wow64SetThreadContext | |
94 | #define Wow64GetThreadSelectorEntry dyn_Wow64GetThreadSelectorEntry | |
2b008701 | 95 | |
43499ea3 PA |
96 | typedef BOOL WINAPI (AdjustTokenPrivileges_ftype) (HANDLE, BOOL, |
97 | PTOKEN_PRIVILEGES, | |
98 | DWORD, PTOKEN_PRIVILEGES, | |
99 | PDWORD); | |
100 | static AdjustTokenPrivileges_ftype *AdjustTokenPrivileges; | |
101 | ||
102 | typedef BOOL WINAPI (DebugActiveProcessStop_ftype) (DWORD); | |
103 | static DebugActiveProcessStop_ftype *DebugActiveProcessStop; | |
104 | ||
105 | typedef BOOL WINAPI (DebugBreakProcess_ftype) (HANDLE); | |
106 | static DebugBreakProcess_ftype *DebugBreakProcess; | |
107 | ||
108 | typedef BOOL WINAPI (DebugSetProcessKillOnExit_ftype) (BOOL); | |
109 | static DebugSetProcessKillOnExit_ftype *DebugSetProcessKillOnExit; | |
110 | ||
111 | typedef BOOL WINAPI (EnumProcessModules_ftype) (HANDLE, HMODULE *, DWORD, | |
112 | LPDWORD); | |
113 | static EnumProcessModules_ftype *EnumProcessModules; | |
114 | ||
46f9f931 HD |
115 | #ifdef __x86_64__ |
116 | typedef BOOL WINAPI (EnumProcessModulesEx_ftype) (HANDLE, HMODULE *, DWORD, | |
117 | LPDWORD, DWORD); | |
118 | static EnumProcessModulesEx_ftype *EnumProcessModulesEx; | |
119 | #endif | |
120 | ||
43499ea3 PA |
121 | typedef BOOL WINAPI (GetModuleInformation_ftype) (HANDLE, HMODULE, |
122 | LPMODULEINFO, DWORD); | |
123 | static GetModuleInformation_ftype *GetModuleInformation; | |
124 | ||
125 | typedef BOOL WINAPI (LookupPrivilegeValueA_ftype) (LPCSTR, LPCSTR, PLUID); | |
126 | static LookupPrivilegeValueA_ftype *LookupPrivilegeValueA; | |
127 | ||
128 | typedef BOOL WINAPI (OpenProcessToken_ftype) (HANDLE, DWORD, PHANDLE); | |
129 | static OpenProcessToken_ftype *OpenProcessToken; | |
130 | ||
131 | typedef BOOL WINAPI (GetCurrentConsoleFont_ftype) (HANDLE, BOOL, | |
132 | CONSOLE_FONT_INFO *); | |
133 | static GetCurrentConsoleFont_ftype *GetCurrentConsoleFont; | |
134 | ||
135 | typedef COORD WINAPI (GetConsoleFontSize_ftype) (HANDLE, DWORD); | |
136 | static GetConsoleFontSize_ftype *GetConsoleFontSize; | |
2b008701 | 137 | |
46f9f931 HD |
138 | #ifdef __x86_64__ |
139 | typedef DWORD WINAPI (Wow64SuspendThread_ftype) (HANDLE); | |
140 | static Wow64SuspendThread_ftype *Wow64SuspendThread; | |
141 | ||
142 | typedef BOOL WINAPI (Wow64GetThreadContext_ftype) (HANDLE, PWOW64_CONTEXT); | |
143 | static Wow64GetThreadContext_ftype *Wow64GetThreadContext; | |
144 | ||
145 | typedef BOOL WINAPI (Wow64SetThreadContext_ftype) (HANDLE, | |
146 | const WOW64_CONTEXT *); | |
147 | static Wow64SetThreadContext_ftype *Wow64SetThreadContext; | |
148 | ||
149 | typedef BOOL WINAPI (Wow64GetThreadSelectorEntry_ftype) (HANDLE, DWORD, | |
150 | PLDT_ENTRY); | |
151 | static Wow64GetThreadSelectorEntry_ftype *Wow64GetThreadSelectorEntry; | |
152 | #endif | |
153 | ||
b3c613f2 CF |
154 | #undef STARTUPINFO |
155 | #undef CreateProcess | |
156 | #undef GetModuleFileNameEx | |
157 | ||
158 | #ifndef __CYGWIN__ | |
159 | # define __PMAX (MAX_PATH + 1) | |
43499ea3 PA |
160 | typedef DWORD WINAPI (GetModuleFileNameEx_ftype) (HANDLE, HMODULE, LPSTR, DWORD); |
161 | static GetModuleFileNameEx_ftype *GetModuleFileNameEx; | |
b3c613f2 CF |
162 | # define STARTUPINFO STARTUPINFOA |
163 | # define CreateProcess CreateProcessA | |
164 | # define GetModuleFileNameEx_name "GetModuleFileNameExA" | |
165 | # define bad_GetModuleFileNameEx bad_GetModuleFileNameExA | |
166 | #else | |
167 | # define __PMAX PATH_MAX | |
581e13c1 | 168 | /* The starting and ending address of the cygwin1.dll text segment. */ |
b3c613f2 CF |
169 | static CORE_ADDR cygwin_load_start; |
170 | static CORE_ADDR cygwin_load_end; | |
b3c613f2 CF |
171 | # define __USEWIDE |
172 | typedef wchar_t cygwin_buf_t; | |
43499ea3 PA |
173 | typedef DWORD WINAPI (GetModuleFileNameEx_ftype) (HANDLE, HMODULE, |
174 | LPWSTR, DWORD); | |
175 | static GetModuleFileNameEx_ftype *GetModuleFileNameEx; | |
b3c613f2 CF |
176 | # define STARTUPINFO STARTUPINFOW |
177 | # define CreateProcess CreateProcessW | |
178 | # define GetModuleFileNameEx_name "GetModuleFileNameExW" | |
179 | # define bad_GetModuleFileNameEx bad_GetModuleFileNameExW | |
10325bc5 | 180 | #endif |
a244bdca | 181 | |
581e13c1 MS |
182 | static int have_saved_context; /* True if we've saved context from a |
183 | cygwin signal. */ | |
15766370 | 184 | #ifdef __CYGWIN__ |
85102364 | 185 | static CONTEXT saved_context; /* Contains the saved context from a |
581e13c1 | 186 | cygwin signal. */ |
15766370 | 187 | #endif |
a244bdca | 188 | |
0714f9bf SS |
189 | /* If we're not using the old Cygwin header file set, define the |
190 | following which never should have been in the generic Win32 API | |
581e13c1 | 191 | headers in the first place since they were our own invention... */ |
0714f9bf | 192 | #ifndef _GNU_H_WINDOWS_H |
9d3789f7 | 193 | enum |
8e860359 CF |
194 | { |
195 | FLAG_TRACE_BIT = 0x100, | |
8e860359 | 196 | }; |
0714f9bf SS |
197 | #endif |
198 | ||
5851ab76 JB |
199 | #ifndef CONTEXT_EXTENDED_REGISTERS |
200 | /* This macro is only defined on ia32. It only makes sense on this target, | |
201 | so define it as zero if not already defined. */ | |
202 | #define CONTEXT_EXTENDED_REGISTERS 0 | |
203 | #endif | |
204 | ||
f0666312 JT |
205 | #define CONTEXT_DEBUGGER_DR CONTEXT_FULL | CONTEXT_FLOATING_POINT \ |
206 | | CONTEXT_SEGMENTS | CONTEXT_DEBUG_REGISTERS \ | |
207 | | CONTEXT_EXTENDED_REGISTERS | |
97da3b20 | 208 | |
41b4aadc | 209 | static uintptr_t dr[8]; |
87a45c96 CF |
210 | static int debug_registers_changed; |
211 | static int debug_registers_used; | |
16d905e2 CF |
212 | |
213 | static int windows_initialization_done; | |
6537bb24 | 214 | #define DR6_CLEAR_VALUE 0xffff0ff0 |
97da3b20 | 215 | |
24cdb46e РИ |
216 | /* The exception thrown by a program to tell the debugger the name of |
217 | a thread. The exception record contains an ID of a thread and a | |
218 | name to give it. This exception has no documented name, but MSDN | |
219 | dubs it "MS_VC_EXCEPTION" in one code example. */ | |
220 | #define MS_VC_EXCEPTION 0x406d1388 | |
221 | ||
222 | typedef enum | |
223 | { | |
224 | HANDLE_EXCEPTION_UNHANDLED = 0, | |
225 | HANDLE_EXCEPTION_HANDLED, | |
226 | HANDLE_EXCEPTION_IGNORED | |
227 | } handle_exception_result; | |
228 | ||
3cee93ac | 229 | /* The string sent by cygwin when it processes a signal. |
581e13c1 | 230 | FIXME: This should be in a cygwin include file. */ |
3929abe9 CF |
231 | #ifndef _CYGWIN_SIGNAL_STRING |
232 | #define _CYGWIN_SIGNAL_STRING "cYgSiGw00f" | |
233 | #endif | |
3cee93ac | 234 | |
29fe111d | 235 | #define CHECK(x) check (x, __FILE__,__LINE__) |
dfe7f3ac | 236 | #define DEBUG_EXEC(x) if (debug_exec) printf_unfiltered x |
4e52d31c PM |
237 | #define DEBUG_EVENTS(x) if (debug_events) printf_unfiltered x |
238 | #define DEBUG_MEM(x) if (debug_memory) printf_unfiltered x | |
239 | #define DEBUG_EXCEPT(x) if (debug_exceptions) printf_unfiltered x | |
24e60978 | 240 | |
9bb9e8ad PM |
241 | static void cygwin_set_dr (int i, CORE_ADDR addr); |
242 | static void cygwin_set_dr7 (unsigned long val); | |
a961bc18 | 243 | static CORE_ADDR cygwin_get_dr (int i); |
9bb9e8ad | 244 | static unsigned long cygwin_get_dr6 (void); |
a961bc18 | 245 | static unsigned long cygwin_get_dr7 (void); |
9bb9e8ad | 246 | |
93366324 | 247 | static std::vector<windows_thread_info *> thread_list; |
24e60978 | 248 | |
581e13c1 | 249 | /* Counts of things. */ |
24e60978 SC |
250 | static int exception_count = 0; |
251 | static int event_count = 0; | |
dfe7f3ac | 252 | static int saw_create; |
bf25528d | 253 | static int open_process_used = 0; |
46f9f931 HD |
254 | #ifdef __x86_64__ |
255 | static bool wow64_process = false; | |
256 | static bool ignore_first_breakpoint = false; | |
257 | #endif | |
24e60978 | 258 | |
581e13c1 | 259 | /* User options. */ |
491144b5 | 260 | static bool new_console = false; |
10325bc5 | 261 | #ifdef __CYGWIN__ |
491144b5 | 262 | static bool cygwin_exceptions = false; |
10325bc5 | 263 | #endif |
491144b5 CB |
264 | static bool new_group = true; |
265 | static bool debug_exec = false; /* show execution */ | |
266 | static bool debug_events = false; /* show events from kernel */ | |
267 | static bool debug_memory = false; /* show target memory accesses */ | |
268 | static bool debug_exceptions = false; /* show target exceptions */ | |
269 | static bool useshell = false; /* use shell for subprocesses */ | |
dfe7f3ac | 270 | |
7e63b4e4 | 271 | /* This vector maps GDB's idea of a register's number into an offset |
dc05df57 | 272 | in the windows exception context vector. |
24e60978 | 273 | |
3cee93ac | 274 | It also contains the bit mask needed to load the register in question. |
24e60978 | 275 | |
7e63b4e4 JB |
276 | The contents of this table can only be computed by the units |
277 | that provide CPU-specific support for Windows native debugging. | |
278 | These units should set the table by calling | |
dc05df57 | 279 | windows_set_context_register_offsets. |
7e63b4e4 | 280 | |
24e60978 SC |
281 | One day we could read a reg, we could inspect the context we |
282 | already have loaded, if it doesn't have the bit set that we need, | |
283 | we read that set of registers in using GetThreadContext. If the | |
581e13c1 | 284 | context already contains what we need, we just unpack it. Then to |
24e60978 SC |
285 | write a register, first we have to ensure that the context contains |
286 | the other regs of the group, and then we copy the info in and set | |
581e13c1 | 287 | out bit. */ |
24e60978 | 288 | |
7e63b4e4 | 289 | static const int *mappings; |
d3a09475 | 290 | |
d40dc7a8 JB |
291 | /* The function to use in order to determine whether a register is |
292 | a segment register or not. */ | |
293 | static segment_register_p_ftype *segment_register_p; | |
294 | ||
73c13fe6 TT |
295 | /* See windows_nat_target::resume to understand why this is commented |
296 | out. */ | |
297 | #if 0 | |
24e60978 | 298 | /* This vector maps the target's idea of an exception (extracted |
581e13c1 | 299 | from the DEBUG_EVENT structure) to GDB's idea. */ |
24e60978 SC |
300 | |
301 | struct xlate_exception | |
302 | { | |
73c13fe6 | 303 | DWORD them; |
2ea28649 | 304 | enum gdb_signal us; |
24e60978 SC |
305 | }; |
306 | ||
73c13fe6 | 307 | static const struct xlate_exception xlate[] = |
24e60978 | 308 | { |
a493e3e2 PA |
309 | {EXCEPTION_ACCESS_VIOLATION, GDB_SIGNAL_SEGV}, |
310 | {STATUS_STACK_OVERFLOW, GDB_SIGNAL_SEGV}, | |
311 | {EXCEPTION_BREAKPOINT, GDB_SIGNAL_TRAP}, | |
312 | {DBG_CONTROL_C, GDB_SIGNAL_INT}, | |
313 | {EXCEPTION_SINGLE_STEP, GDB_SIGNAL_TRAP}, | |
73c13fe6 TT |
314 | {STATUS_FLOAT_DIVIDE_BY_ZERO, GDB_SIGNAL_FPE} |
315 | }; | |
24e60978 | 316 | |
73c13fe6 | 317 | #endif /* 0 */ |
f6ac5f3d PA |
318 | |
319 | struct windows_nat_target final : public x86_nat_target<inf_child_target> | |
320 | { | |
321 | void close () override; | |
322 | ||
323 | void attach (const char *, int) override; | |
324 | ||
325 | bool attach_no_wait () override | |
326 | { return true; } | |
327 | ||
328 | void detach (inferior *, int) override; | |
329 | ||
330 | void resume (ptid_t, int , enum gdb_signal) override; | |
331 | ||
332 | ptid_t wait (ptid_t, struct target_waitstatus *, int) override; | |
333 | ||
334 | void fetch_registers (struct regcache *, int) override; | |
335 | void store_registers (struct regcache *, int) override; | |
336 | ||
0a4afda3 TT |
337 | bool stopped_by_sw_breakpoint () override |
338 | { | |
3c76026d | 339 | return current_windows_thread->stopped_at_software_breakpoint; |
0a4afda3 TT |
340 | } |
341 | ||
342 | bool supports_stopped_by_sw_breakpoint () override | |
343 | { | |
344 | return true; | |
345 | } | |
346 | ||
f6ac5f3d PA |
347 | enum target_xfer_status xfer_partial (enum target_object object, |
348 | const char *annex, | |
349 | gdb_byte *readbuf, | |
350 | const gdb_byte *writebuf, | |
351 | ULONGEST offset, ULONGEST len, | |
352 | ULONGEST *xfered_len) override; | |
353 | ||
354 | void files_info () override; | |
355 | ||
356 | void kill () override; | |
357 | ||
358 | void create_inferior (const char *, const std::string &, | |
359 | char **, int) override; | |
360 | ||
361 | void mourn_inferior () override; | |
362 | ||
57810aa7 | 363 | bool thread_alive (ptid_t ptid) override; |
f6ac5f3d | 364 | |
a068643d | 365 | std::string pid_to_str (ptid_t) override; |
f6ac5f3d PA |
366 | |
367 | void interrupt () override; | |
368 | ||
369 | char *pid_to_exec_file (int pid) override; | |
370 | ||
371 | ptid_t get_ada_task_ptid (long lwp, long thread) override; | |
372 | ||
57810aa7 | 373 | bool get_tib_address (ptid_t ptid, CORE_ADDR *addr) override; |
f6ac5f3d PA |
374 | |
375 | const char *thread_name (struct thread_info *) override; | |
5b6d1e4f PA |
376 | |
377 | int get_windows_debug_event (int pid, struct target_waitstatus *ourstatus); | |
f6ac5f3d PA |
378 | }; |
379 | ||
380 | static windows_nat_target the_windows_nat_target; | |
381 | ||
7e63b4e4 JB |
382 | /* Set the MAPPINGS static global to OFFSETS. |
383 | See the description of MAPPINGS for more details. */ | |
384 | ||
46f9f931 | 385 | static void |
dc05df57 | 386 | windows_set_context_register_offsets (const int *offsets) |
7e63b4e4 JB |
387 | { |
388 | mappings = offsets; | |
389 | } | |
390 | ||
46f9f931 HD |
391 | /* Set the function that should be used by this module to determine |
392 | whether a given register is a segment register or not. */ | |
d40dc7a8 | 393 | |
46f9f931 | 394 | static void |
d40dc7a8 JB |
395 | windows_set_segment_register_p (segment_register_p_ftype *fun) |
396 | { | |
397 | segment_register_p = fun; | |
398 | } | |
399 | ||
fa4ba8da PM |
400 | static void |
401 | check (BOOL ok, const char *file, int line) | |
402 | { | |
403 | if (!ok) | |
d50a0ce2 CV |
404 | printf_filtered ("error return %s:%d was %u\n", file, line, |
405 | (unsigned) GetLastError ()); | |
fa4ba8da PM |
406 | } |
407 | ||
28688adf | 408 | /* See nat/windows-nat.h. */ |
8e61ebec | 409 | |
28688adf TT |
410 | windows_thread_info * |
411 | windows_nat::thread_rec (ptid_t ptid, thread_disposition_type disposition) | |
24e60978 | 412 | { |
93366324 | 413 | for (windows_thread_info *th : thread_list) |
28688adf | 414 | if (th->tid == ptid.lwp ()) |
3cee93ac | 415 | { |
8e61ebec | 416 | if (!th->suspended) |
3cee93ac | 417 | { |
8e61ebec TT |
418 | switch (disposition) |
419 | { | |
420 | case DONT_INVALIDATE_CONTEXT: | |
421 | /* Nothing. */ | |
422 | break; | |
423 | case INVALIDATE_CONTEXT: | |
28688adf | 424 | if (ptid.lwp () != current_event.dwThreadId) |
8e61ebec TT |
425 | th->suspend (); |
426 | th->reload_context = true; | |
427 | break; | |
428 | case DONT_SUSPEND: | |
429 | th->reload_context = true; | |
430 | th->suspended = -1; | |
431 | break; | |
432 | } | |
3cee93ac CF |
433 | } |
434 | return th; | |
435 | } | |
436 | ||
437 | return NULL; | |
438 | } | |
439 | ||
c559d709 JB |
440 | /* Add a thread to the thread list. |
441 | ||
442 | PTID is the ptid of the thread to be added. | |
443 | H is its Windows handle. | |
444 | TLB is its thread local base. | |
445 | MAIN_THREAD_P should be true if the thread to be added is | |
446 | the main thread, false otherwise. */ | |
447 | ||
876d1cd7 | 448 | static windows_thread_info * |
c559d709 | 449 | windows_add_thread (ptid_t ptid, HANDLE h, void *tlb, bool main_thread_p) |
3cee93ac | 450 | { |
876d1cd7 | 451 | windows_thread_info *th; |
2dc38344 | 452 | |
7c7411bc | 453 | gdb_assert (ptid.lwp () != 0); |
2dc38344 | 454 | |
28688adf | 455 | if ((th = thread_rec (ptid, DONT_INVALIDATE_CONTEXT))) |
3cee93ac CF |
456 | return th; |
457 | ||
e9534bd2 | 458 | CORE_ADDR base = (CORE_ADDR) (uintptr_t) tlb; |
46f9f931 HD |
459 | #ifdef __x86_64__ |
460 | /* For WOW64 processes, this is actually the pointer to the 64bit TIB, | |
461 | and the 32bit TIB is exactly 2 pages after it. */ | |
462 | if (wow64_process) | |
e9534bd2 | 463 | base += 0x2000; |
46f9f931 | 464 | #endif |
28688adf | 465 | th = new windows_thread_info (ptid.lwp (), h, base); |
93366324 | 466 | thread_list.push_back (th); |
c559d709 JB |
467 | |
468 | /* Add this new thread to the list of threads. | |
469 | ||
470 | To be consistent with what's done on other platforms, we add | |
471 | the main thread silently (in reality, this thread is really | |
472 | more of a process to the user than a thread). */ | |
473 | if (main_thread_p) | |
5b6d1e4f | 474 | add_thread_silent (&the_windows_nat_target, ptid); |
c559d709 | 475 | else |
5b6d1e4f | 476 | add_thread (&the_windows_nat_target, ptid); |
c559d709 | 477 | |
2dc38344 | 478 | /* Set the debug registers for the new thread if they are used. */ |
fa4ba8da PM |
479 | if (debug_registers_used) |
480 | { | |
46f9f931 HD |
481 | #ifdef __x86_64__ |
482 | if (wow64_process) | |
483 | { | |
484 | /* Only change the value of the debug registers. */ | |
485 | th->wow64_context.ContextFlags = CONTEXT_DEBUG_REGISTERS; | |
486 | CHECK (Wow64GetThreadContext (th->h, &th->wow64_context)); | |
487 | th->wow64_context.Dr0 = dr[0]; | |
488 | th->wow64_context.Dr1 = dr[1]; | |
489 | th->wow64_context.Dr2 = dr[2]; | |
490 | th->wow64_context.Dr3 = dr[3]; | |
491 | th->wow64_context.Dr6 = DR6_CLEAR_VALUE; | |
492 | th->wow64_context.Dr7 = dr[7]; | |
493 | CHECK (Wow64SetThreadContext (th->h, &th->wow64_context)); | |
494 | th->wow64_context.ContextFlags = 0; | |
495 | } | |
496 | else | |
497 | #endif | |
498 | { | |
499 | /* Only change the value of the debug registers. */ | |
500 | th->context.ContextFlags = CONTEXT_DEBUG_REGISTERS; | |
501 | CHECK (GetThreadContext (th->h, &th->context)); | |
502 | th->context.Dr0 = dr[0]; | |
503 | th->context.Dr1 = dr[1]; | |
504 | th->context.Dr2 = dr[2]; | |
505 | th->context.Dr3 = dr[3]; | |
506 | th->context.Dr6 = DR6_CLEAR_VALUE; | |
507 | th->context.Dr7 = dr[7]; | |
508 | CHECK (SetThreadContext (th->h, &th->context)); | |
509 | th->context.ContextFlags = 0; | |
510 | } | |
fa4ba8da | 511 | } |
3cee93ac | 512 | return th; |
24e60978 SC |
513 | } |
514 | ||
26c4b26f | 515 | /* Clear out any old thread list and reinitialize it to a |
581e13c1 | 516 | pristine state. */ |
24e60978 | 517 | static void |
dc05df57 | 518 | windows_init_thread_list (void) |
24e60978 | 519 | { |
dc05df57 | 520 | DEBUG_EVENTS (("gdb: windows_init_thread_list\n")); |
3cee93ac | 521 | init_thread_list (); |
93366324 TT |
522 | |
523 | for (windows_thread_info *here : thread_list) | |
e9534bd2 | 524 | delete here; |
93366324 TT |
525 | |
526 | thread_list.clear (); | |
3cee93ac CF |
527 | } |
528 | ||
c559d709 JB |
529 | /* Delete a thread from the list of threads. |
530 | ||
531 | PTID is the ptid of the thread to be deleted. | |
532 | EXIT_CODE is the thread's exit code. | |
533 | MAIN_THREAD_P should be true if the thread to be deleted is | |
534 | the main thread, false otherwise. */ | |
535 | ||
3cee93ac | 536 | static void |
c559d709 | 537 | windows_delete_thread (ptid_t ptid, DWORD exit_code, bool main_thread_p) |
3cee93ac | 538 | { |
2dc38344 PA |
539 | DWORD id; |
540 | ||
7c7411bc | 541 | gdb_assert (ptid.lwp () != 0); |
2dc38344 | 542 | |
7c7411bc | 543 | id = ptid.lwp (); |
3cee93ac | 544 | |
c559d709 JB |
545 | /* Emit a notification about the thread being deleted. |
546 | ||
547 | Note that no notification was printed when the main thread | |
548 | was created, and thus, unless in verbose mode, we should be | |
85102364 | 549 | symmetrical, and avoid that notification for the main thread |
c559d709 JB |
550 | here as well. */ |
551 | ||
3cee93ac | 552 | if (info_verbose) |
a068643d | 553 | printf_unfiltered ("[Deleting %s]\n", target_pid_to_str (ptid).c_str ()); |
c559d709 | 554 | else if (print_thread_events && !main_thread_p) |
e0ea48a0 | 555 | printf_unfiltered (_("[%s exited with code %u]\n"), |
a068643d TT |
556 | target_pid_to_str (ptid).c_str (), |
557 | (unsigned) exit_code); | |
c559d709 | 558 | |
5b6d1e4f | 559 | delete_thread (find_thread_ptid (&the_windows_nat_target, ptid)); |
3cee93ac | 560 | |
93366324 TT |
561 | auto iter = std::find_if (thread_list.begin (), thread_list.end (), |
562 | [=] (windows_thread_info *th) | |
563 | { | |
55a1e039 | 564 | return th->tid == id; |
93366324 | 565 | }); |
3cee93ac | 566 | |
93366324 | 567 | if (iter != thread_list.end ()) |
24e60978 | 568 | { |
e9534bd2 | 569 | delete *iter; |
93366324 | 570 | thread_list.erase (iter); |
24e60978 SC |
571 | } |
572 | } | |
573 | ||
9a325b7b JB |
574 | /* Fetches register number R from the given windows_thread_info, |
575 | and supplies its value to the given regcache. | |
576 | ||
577 | This function assumes that R is non-negative. A failed assertion | |
578 | is raised if that is not true. | |
579 | ||
580 | This function assumes that TH->RELOAD_CONTEXT is not set, meaning | |
581 | that the windows_thread_info has an up-to-date context. A failed | |
582 | assertion is raised if that assumption is violated. */ | |
583 | ||
3cee93ac | 584 | static void |
9a325b7b JB |
585 | windows_fetch_one_register (struct regcache *regcache, |
586 | windows_thread_info *th, int r) | |
24e60978 | 587 | { |
9a325b7b JB |
588 | gdb_assert (r >= 0); |
589 | gdb_assert (!th->reload_context); | |
590 | ||
46f9f931 HD |
591 | char *context_ptr = (char *) &th->context; |
592 | #ifdef __x86_64__ | |
593 | if (wow64_process) | |
594 | context_ptr = (char *) &th->wow64_context; | |
595 | #endif | |
596 | ||
597 | char *context_offset = context_ptr + mappings[r]; | |
ac7936df | 598 | struct gdbarch *gdbarch = regcache->arch (); |
20a6ec49 | 599 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); |
9a325b7b | 600 | |
0a4afda3 TT |
601 | gdb_assert (!gdbarch_read_pc_p (gdbarch)); |
602 | gdb_assert (gdbarch_pc_regnum (gdbarch) >= 0); | |
603 | gdb_assert (!gdbarch_write_pc_p (gdbarch)); | |
604 | ||
9a325b7b JB |
605 | if (r == I387_FISEG_REGNUM (tdep)) |
606 | { | |
607 | long l = *((long *) context_offset) & 0xffff; | |
608 | regcache->raw_supply (r, (char *) &l); | |
609 | } | |
610 | else if (r == I387_FOP_REGNUM (tdep)) | |
611 | { | |
612 | long l = (*((long *) context_offset) >> 16) & ((1 << 11) - 1); | |
613 | regcache->raw_supply (r, (char *) &l); | |
614 | } | |
615 | else if (segment_register_p (r)) | |
616 | { | |
617 | /* GDB treats segment registers as 32bit registers, but they are | |
618 | in fact only 16 bits long. Make sure we do not read extra | |
619 | bits from our source buffer. */ | |
620 | long l = *((long *) context_offset) & 0xffff; | |
621 | regcache->raw_supply (r, (char *) &l); | |
622 | } | |
623 | else | |
0a4afda3 TT |
624 | { |
625 | if (th->stopped_at_software_breakpoint | |
626 | && r == gdbarch_pc_regnum (gdbarch)) | |
627 | { | |
628 | int size = register_size (gdbarch, r); | |
629 | if (size == 4) | |
630 | { | |
631 | uint32_t value; | |
632 | memcpy (&value, context_offset, size); | |
633 | value -= gdbarch_decr_pc_after_break (gdbarch); | |
634 | memcpy (context_offset, &value, size); | |
635 | } | |
636 | else | |
637 | { | |
638 | gdb_assert (size == 8); | |
639 | uint64_t value; | |
640 | memcpy (&value, context_offset, size); | |
641 | value -= gdbarch_decr_pc_after_break (gdbarch); | |
642 | memcpy (context_offset, &value, size); | |
643 | } | |
644 | } | |
645 | regcache->raw_supply (r, context_offset); | |
646 | } | |
9a325b7b JB |
647 | } |
648 | ||
649 | void | |
650 | windows_nat_target::fetch_registers (struct regcache *regcache, int r) | |
651 | { | |
28688adf | 652 | windows_thread_info *th = thread_rec (regcache->ptid (), INVALIDATE_CONTEXT); |
9a325b7b JB |
653 | |
654 | /* Check if TH exists. Windows sometimes uses a non-existent | |
655 | thread id in its events. */ | |
656 | if (th == NULL) | |
657 | return; | |
6c7de422 | 658 | |
3de88e9a | 659 | if (th->reload_context) |
3ade5333 | 660 | { |
f20c58f5 | 661 | #ifdef __CYGWIN__ |
a244bdca CF |
662 | if (have_saved_context) |
663 | { | |
581e13c1 MS |
664 | /* Lie about where the program actually is stopped since |
665 | cygwin has informed us that we should consider the signal | |
666 | to have occurred at another location which is stored in | |
667 | "saved_context. */ | |
3de88e9a | 668 | memcpy (&th->context, &saved_context, |
581e13c1 | 669 | __COPY_CONTEXT_SIZE); |
a244bdca CF |
670 | have_saved_context = 0; |
671 | } | |
672 | else | |
46f9f931 HD |
673 | #endif |
674 | #ifdef __x86_64__ | |
675 | if (wow64_process) | |
676 | { | |
677 | th->wow64_context.ContextFlags = CONTEXT_DEBUGGER_DR; | |
678 | CHECK (Wow64GetThreadContext (th->h, &th->wow64_context)); | |
679 | /* Copy dr values from that thread. | |
680 | But only if there were not modified since last stop. | |
681 | PR gdb/2388 */ | |
682 | if (!debug_registers_changed) | |
683 | { | |
684 | dr[0] = th->wow64_context.Dr0; | |
685 | dr[1] = th->wow64_context.Dr1; | |
686 | dr[2] = th->wow64_context.Dr2; | |
687 | dr[3] = th->wow64_context.Dr3; | |
688 | dr[6] = th->wow64_context.Dr6; | |
689 | dr[7] = th->wow64_context.Dr7; | |
690 | } | |
691 | } | |
692 | else | |
cb832706 | 693 | #endif |
a244bdca | 694 | { |
a244bdca | 695 | th->context.ContextFlags = CONTEXT_DEBUGGER_DR; |
17617f2d | 696 | CHECK (GetThreadContext (th->h, &th->context)); |
2b008701 | 697 | /* Copy dr values from that thread. |
581e13c1 MS |
698 | But only if there were not modified since last stop. |
699 | PR gdb/2388 */ | |
88616312 PM |
700 | if (!debug_registers_changed) |
701 | { | |
702 | dr[0] = th->context.Dr0; | |
703 | dr[1] = th->context.Dr1; | |
704 | dr[2] = th->context.Dr2; | |
705 | dr[3] = th->context.Dr3; | |
706 | dr[6] = th->context.Dr6; | |
707 | dr[7] = th->context.Dr7; | |
708 | } | |
a244bdca | 709 | } |
62fe396b | 710 | th->reload_context = false; |
3ade5333 CF |
711 | } |
712 | ||
9a325b7b JB |
713 | if (r < 0) |
714 | for (r = 0; r < gdbarch_num_regs (regcache->arch()); r++) | |
715 | windows_fetch_one_register (regcache, th, r); | |
3cee93ac | 716 | else |
9a325b7b | 717 | windows_fetch_one_register (regcache, th, r); |
3cee93ac CF |
718 | } |
719 | ||
9a325b7b JB |
720 | /* Collect the register number R from the given regcache, and store |
721 | its value into the corresponding area of the given thread's context. | |
3de88e9a | 722 | |
9a325b7b JB |
723 | This function assumes that R is non-negative. A failed assertion |
724 | assertion is raised if that is not true. */ | |
3cee93ac CF |
725 | |
726 | static void | |
9a325b7b JB |
727 | windows_store_one_register (const struct regcache *regcache, |
728 | windows_thread_info *th, int r) | |
3cee93ac | 729 | { |
9a325b7b JB |
730 | gdb_assert (r >= 0); |
731 | ||
46f9f931 HD |
732 | char *context_ptr = (char *) &th->context; |
733 | #ifdef __x86_64__ | |
734 | if (wow64_process) | |
735 | context_ptr = (char *) &th->wow64_context; | |
736 | #endif | |
737 | ||
738 | regcache->raw_collect (r, context_ptr + mappings[r]); | |
24e60978 SC |
739 | } |
740 | ||
3de88e9a SM |
741 | /* Store a new register value into the context of the thread tied to |
742 | REGCACHE. */ | |
f6ac5f3d PA |
743 | |
744 | void | |
745 | windows_nat_target::store_registers (struct regcache *regcache, int r) | |
3cee93ac | 746 | { |
28688adf | 747 | windows_thread_info *th = thread_rec (regcache->ptid (), INVALIDATE_CONTEXT); |
3de88e9a SM |
748 | |
749 | /* Check if TH exists. Windows sometimes uses a non-existent | |
581e13c1 | 750 | thread id in its events. */ |
9a325b7b JB |
751 | if (th == NULL) |
752 | return; | |
753 | ||
754 | if (r < 0) | |
755 | for (r = 0; r < gdbarch_num_regs (regcache->arch ()); r++) | |
756 | windows_store_one_register (regcache, th, r); | |
757 | else | |
758 | windows_store_one_register (regcache, th, r); | |
3cee93ac | 759 | } |
24e60978 | 760 | |
581e13c1 | 761 | /* Maintain a linked list of "so" information. */ |
d0e449a1 | 762 | struct lm_info_windows : public lm_info_base |
02e423b9 | 763 | { |
f8fdb78e | 764 | LPVOID load_addr = 0; |
c162ed3e | 765 | CORE_ADDR text_offset = 0; |
3ee6f623 CF |
766 | }; |
767 | ||
768 | static struct so_list solib_start, *solib_end; | |
02e423b9 | 769 | |
de1b3c3d | 770 | static struct so_list * |
dc05df57 | 771 | windows_make_so (const char *name, LPVOID load_addr) |
8e860359 | 772 | { |
3ee6f623 | 773 | struct so_list *so; |
d0d0ab16 CV |
774 | char *p; |
775 | #ifndef __CYGWIN__ | |
b3c613f2 CF |
776 | char buf[__PMAX]; |
777 | char cwd[__PMAX]; | |
3f8ad85b CF |
778 | WIN32_FIND_DATA w32_fd; |
779 | HANDLE h = FindFirstFile(name, &w32_fd); | |
3f8ad85b | 780 | |
6badb179 CF |
781 | if (h == INVALID_HANDLE_VALUE) |
782 | strcpy (buf, name); | |
783 | else | |
3f8ad85b | 784 | { |
c914e0cc CF |
785 | FindClose (h); |
786 | strcpy (buf, name); | |
787 | if (GetCurrentDirectory (MAX_PATH + 1, cwd)) | |
788 | { | |
789 | p = strrchr (buf, '\\'); | |
790 | if (p) | |
791 | p[1] = '\0'; | |
792 | SetCurrentDirectory (buf); | |
793 | GetFullPathName (w32_fd.cFileName, MAX_PATH, buf, &p); | |
794 | SetCurrentDirectory (cwd); | |
795 | } | |
3f8ad85b | 796 | } |
3ee6f623 CF |
797 | if (strcasecmp (buf, "ntdll.dll") == 0) |
798 | { | |
799 | GetSystemDirectory (buf, sizeof (buf)); | |
800 | strcat (buf, "\\ntdll.dll"); | |
801 | } | |
d0d0ab16 | 802 | #else |
b3c613f2 | 803 | cygwin_buf_t buf[__PMAX]; |
d0d0ab16 | 804 | |
b3c613f2 | 805 | buf[0] = 0; |
d0d0ab16 CV |
806 | if (access (name, F_OK) != 0) |
807 | { | |
808 | if (strcasecmp (name, "ntdll.dll") == 0) | |
b3c613f2 | 809 | #ifdef __USEWIDE |
d0d0ab16 CV |
810 | { |
811 | GetSystemDirectoryW (buf, sizeof (buf) / sizeof (wchar_t)); | |
812 | wcscat (buf, L"\\ntdll.dll"); | |
813 | } | |
b3c613f2 CF |
814 | #else |
815 | { | |
816 | GetSystemDirectoryA (buf, sizeof (buf) / sizeof (wchar_t)); | |
817 | strcat (buf, "\\ntdll.dll"); | |
818 | } | |
819 | #endif | |
d0d0ab16 CV |
820 | } |
821 | #endif | |
41bf6aca | 822 | so = XCNEW (struct so_list); |
f8fdb78e | 823 | lm_info_windows *li = new lm_info_windows; |
d0e449a1 SM |
824 | so->lm_info = li; |
825 | li->load_addr = load_addr; | |
de1b3c3d | 826 | strcpy (so->so_original_name, name); |
10325bc5 PA |
827 | #ifndef __CYGWIN__ |
828 | strcpy (so->so_name, buf); | |
829 | #else | |
d0d0ab16 CV |
830 | if (buf[0]) |
831 | cygwin_conv_path (CCP_WIN_W_TO_POSIX, buf, so->so_name, | |
832 | SO_NAME_MAX_PATH_SIZE); | |
833 | else | |
834 | { | |
60c5c021 | 835 | char *rname = realpath (name, NULL); |
d0d0ab16 CV |
836 | if (rname && strlen (rname) < SO_NAME_MAX_PATH_SIZE) |
837 | { | |
838 | strcpy (so->so_name, rname); | |
839 | free (rname); | |
840 | } | |
841 | else | |
842 | error (_("dll path too long")); | |
843 | } | |
de1b3c3d PA |
844 | /* Record cygwin1.dll .text start/end. */ |
845 | p = strchr (so->so_name, '\0') - (sizeof ("/cygwin1.dll") - 1); | |
846 | if (p >= so->so_name && strcasecmp (p, "/cygwin1.dll") == 0) | |
847 | { | |
de1b3c3d | 848 | asection *text = NULL; |
8e860359 | 849 | |
192b62ce | 850 | gdb_bfd_ref_ptr abfd (gdb_bfd_open (so->so_name, "pei-i386", -1)); |
a244bdca | 851 | |
192b62ce | 852 | if (abfd == NULL) |
de1b3c3d PA |
853 | return so; |
854 | ||
192b62ce TT |
855 | if (bfd_check_format (abfd.get (), bfd_object)) |
856 | text = bfd_get_section_by_name (abfd.get (), ".text"); | |
de1b3c3d PA |
857 | |
858 | if (!text) | |
192b62ce | 859 | return so; |
de1b3c3d | 860 | |
7a9dd1b2 | 861 | /* The symbols in a dll are offset by 0x1000, which is the |
de1b3c3d | 862 | offset from 0 of the first byte in an image - because of the |
581e13c1 MS |
863 | file header and the section alignment. */ |
864 | cygwin_load_start = (CORE_ADDR) (uintptr_t) ((char *) | |
865 | load_addr + 0x1000); | |
fd361982 | 866 | cygwin_load_end = cygwin_load_start + bfd_section_size (text); |
de1b3c3d | 867 | } |
10325bc5 | 868 | #endif |
de1b3c3d PA |
869 | |
870 | return so; | |
8e860359 CF |
871 | } |
872 | ||
1cd9feab JB |
873 | /* Handle a DLL load event, and return 1. |
874 | ||
875 | This function assumes that this event did not occur during inferior | |
876 | initialization, where their event info may be incomplete (see | |
877 | do_initial_windows_stuff and windows_add_all_dlls for more info | |
878 | on how we handle DLL loading during that phase). */ | |
879 | ||
bf469271 PA |
880 | static void |
881 | handle_load_dll () | |
24e60978 | 882 | { |
3a4b77d8 | 883 | LOAD_DLL_DEBUG_INFO *event = ¤t_event.u.LoadDll; |
9d8679cc | 884 | const char *dll_name; |
24e60978 | 885 | |
94481b8c JB |
886 | /* Try getting the DLL name via the lpImageName field of the event. |
887 | Note that Microsoft documents this fields as strictly optional, | |
888 | in the sense that it might be NULL. And the first DLL event in | |
889 | particular is explicitly documented as "likely not pass[ed]" | |
890 | (source: MSDN LOAD_DLL_DEBUG_INFO structure). */ | |
1cd9feab JB |
891 | dll_name = get_image_name (current_process_handle, |
892 | event->lpImageName, event->fUnicode); | |
3cee93ac | 893 | if (!dll_name) |
bf469271 | 894 | return; |
3cee93ac | 895 | |
dc05df57 | 896 | solib_end->next = windows_make_so (dll_name, event->lpBaseOfDll); |
de1b3c3d | 897 | solib_end = solib_end->next; |
450005e7 | 898 | |
d0e449a1 SM |
899 | lm_info_windows *li = (lm_info_windows *) solib_end->lm_info; |
900 | ||
a74ce742 | 901 | DEBUG_EVENTS (("gdb: Loading dll \"%s\" at %s.\n", solib_end->so_name, |
d0e449a1 | 902 | host_address_to_string (li->load_addr))); |
450005e7 CF |
903 | } |
904 | ||
3ee6f623 | 905 | static void |
dc05df57 | 906 | windows_free_so (struct so_list *so) |
3ee6f623 | 907 | { |
f8fdb78e SM |
908 | lm_info_windows *li = (lm_info_windows *) so->lm_info; |
909 | ||
910 | delete li; | |
de1b3c3d | 911 | xfree (so); |
3cb8e7f6 CF |
912 | } |
913 | ||
3be75f87 JB |
914 | /* Handle a DLL unload event. |
915 | Return 1 if successful, or zero otherwise. | |
916 | ||
917 | This function assumes that this event did not occur during inferior | |
918 | initialization, where their event info may be incomplete (see | |
919 | do_initial_windows_stuff and windows_add_all_dlls for more info | |
920 | on how we handle DLL loading during that phase). */ | |
921 | ||
bf469271 PA |
922 | static void |
923 | handle_unload_dll () | |
d3ff4a77 | 924 | { |
d3653bf6 | 925 | LPVOID lpBaseOfDll = current_event.u.UnloadDll.lpBaseOfDll; |
3ee6f623 | 926 | struct so_list *so; |
d3ff4a77 CF |
927 | |
928 | for (so = &solib_start; so->next != NULL; so = so->next) | |
d0e449a1 SM |
929 | { |
930 | lm_info_windows *li_next = (lm_info_windows *) so->next->lm_info; | |
a25cd31f | 931 | |
d0e449a1 SM |
932 | if (li_next->load_addr == lpBaseOfDll) |
933 | { | |
934 | struct so_list *sodel = so->next; | |
7488902c | 935 | |
d0e449a1 SM |
936 | so->next = sodel->next; |
937 | if (!so->next) | |
938 | solib_end = so; | |
939 | DEBUG_EVENTS (("gdb: Unloading dll \"%s\".\n", sodel->so_name)); | |
940 | ||
941 | windows_free_so (sodel); | |
bf469271 | 942 | return; |
d0e449a1 SM |
943 | } |
944 | } | |
3929abe9 | 945 | |
ecc13e53 JB |
946 | /* We did not find any DLL that was previously loaded at this address, |
947 | so register a complaint. We do not report an error, because we have | |
948 | observed that this may be happening under some circumstances. For | |
949 | instance, running 32bit applications on x64 Windows causes us to receive | |
950 | 4 mysterious UNLOAD_DLL_DEBUG_EVENTs during the startup phase (these | |
951 | events are apparently caused by the WOW layer, the interface between | |
952 | 32bit and 64bit worlds). */ | |
b98664d3 | 953 | complaint (_("dll starting at %s not found."), |
ecc13e53 | 954 | host_address_to_string (lpBaseOfDll)); |
bf469271 PA |
955 | } |
956 | ||
957 | /* Call FUNC wrapped in a TRY/CATCH that swallows all GDB | |
958 | exceptions. */ | |
d3ff4a77 | 959 | |
bf469271 PA |
960 | static void |
961 | catch_errors (void (*func) ()) | |
962 | { | |
a70b8144 | 963 | try |
bf469271 PA |
964 | { |
965 | func (); | |
966 | } | |
230d2906 | 967 | catch (const gdb_exception &ex) |
bf469271 PA |
968 | { |
969 | exception_print (gdb_stderr, ex); | |
970 | } | |
d3ff4a77 CF |
971 | } |
972 | ||
581e13c1 | 973 | /* Clear list of loaded DLLs. */ |
3ee6f623 | 974 | static void |
dc05df57 | 975 | windows_clear_solib (void) |
450005e7 | 976 | { |
25057eb0 HD |
977 | struct so_list *so; |
978 | ||
979 | for (so = solib_start.next; so; so = solib_start.next) | |
980 | { | |
981 | solib_start.next = so->next; | |
982 | windows_free_so (so); | |
983 | } | |
984 | ||
450005e7 | 985 | solib_end = &solib_start; |
450005e7 | 986 | } |
295732ea | 987 | |
463888ab | 988 | static void |
0b39b52e | 989 | signal_event_command (const char *args, int from_tty) |
463888ab РИ |
990 | { |
991 | uintptr_t event_id = 0; | |
992 | char *endargs = NULL; | |
993 | ||
994 | if (args == NULL) | |
995 | error (_("signal-event requires an argument (integer event id)")); | |
996 | ||
997 | event_id = strtoumax (args, &endargs, 10); | |
998 | ||
999 | if ((errno == ERANGE) || (event_id == 0) || (event_id > UINTPTR_MAX) || | |
1000 | ((HANDLE) event_id == INVALID_HANDLE_VALUE)) | |
1001 | error (_("Failed to convert `%s' to event id"), args); | |
1002 | ||
1003 | SetEvent ((HANDLE) event_id); | |
1004 | CloseHandle ((HANDLE) event_id); | |
1005 | } | |
1006 | ||
d41b524f TT |
1007 | /* See nat/windows-nat.h. */ |
1008 | ||
1009 | int | |
1010 | windows_nat::handle_output_debug_string (struct target_waitstatus *ourstatus) | |
3cee93ac | 1011 | { |
e83e4e24 | 1012 | gdb::unique_xmalloc_ptr<char> s; |
a244bdca | 1013 | int retval = 0; |
3cee93ac CF |
1014 | |
1015 | if (!target_read_string | |
2c647436 | 1016 | ((CORE_ADDR) (uintptr_t) current_event.u.DebugString.lpDebugStringData, |
e83e4e24 TT |
1017 | &s, 1024, 0) |
1018 | || !s || !*(s.get ())) | |
a244bdca | 1019 | /* nothing to do */; |
e83e4e24 | 1020 | else if (!startswith (s.get (), _CYGWIN_SIGNAL_STRING)) |
3cee93ac | 1021 | { |
10325bc5 | 1022 | #ifdef __CYGWIN__ |
e83e4e24 | 1023 | if (!startswith (s.get (), "cYg")) |
10325bc5 | 1024 | #endif |
040ea00b | 1025 | { |
e83e4e24 | 1026 | char *p = strchr (s.get (), '\0'); |
040ea00b | 1027 | |
e83e4e24 | 1028 | if (p > s.get () && *--p == '\n') |
040ea00b | 1029 | *p = '\0'; |
e83e4e24 | 1030 | warning (("%s"), s.get ()); |
040ea00b | 1031 | } |
3cee93ac | 1032 | } |
f20c58f5 | 1033 | #ifdef __CYGWIN__ |
d3a09475 | 1034 | else |
3cee93ac | 1035 | { |
581e13c1 MS |
1036 | /* Got a cygwin signal marker. A cygwin signal is followed by |
1037 | the signal number itself and then optionally followed by the | |
1038 | thread id and address to saved context within the DLL. If | |
1039 | these are supplied, then the given thread is assumed to have | |
1040 | issued the signal and the context from the thread is assumed | |
1041 | to be stored at the given address in the inferior. Tell gdb | |
1042 | to treat this like a real signal. */ | |
3cee93ac | 1043 | char *p; |
e83e4e24 | 1044 | int sig = strtol (s.get () + sizeof (_CYGWIN_SIGNAL_STRING) - 1, &p, 0); |
0ae534d2 | 1045 | gdb_signal gotasig = gdb_signal_from_host (sig); |
c62fa0e2 | 1046 | |
0714f9bf SS |
1047 | ourstatus->value.sig = gotasig; |
1048 | if (gotasig) | |
a244bdca CF |
1049 | { |
1050 | LPCVOID x; | |
2c15ef43 | 1051 | SIZE_T n; |
c62fa0e2 | 1052 | |
a244bdca CF |
1053 | ourstatus->kind = TARGET_WAITKIND_STOPPED; |
1054 | retval = strtoul (p, &p, 0); | |
1055 | if (!retval) | |
ab4ee614 | 1056 | retval = current_event.dwThreadId; |
40653b35 | 1057 | else if ((x = (LPCVOID) (uintptr_t) strtoull (p, NULL, 0)) |
a244bdca | 1058 | && ReadProcessMemory (current_process_handle, x, |
581e13c1 MS |
1059 | &saved_context, |
1060 | __COPY_CONTEXT_SIZE, &n) | |
a244bdca CF |
1061 | && n == __COPY_CONTEXT_SIZE) |
1062 | have_saved_context = 1; | |
a244bdca | 1063 | } |
3cee93ac | 1064 | } |
cb832706 | 1065 | #endif |
3cee93ac | 1066 | |
a244bdca | 1067 | return retval; |
3cee93ac | 1068 | } |
24e60978 | 1069 | |
c1748f97 PM |
1070 | static int |
1071 | display_selector (HANDLE thread, DWORD sel) | |
1072 | { | |
1073 | LDT_ENTRY info; | |
46f9f931 HD |
1074 | BOOL ret; |
1075 | #ifdef __x86_64__ | |
1076 | if (wow64_process) | |
1077 | ret = Wow64GetThreadSelectorEntry (thread, sel, &info); | |
1078 | else | |
1079 | #endif | |
1080 | ret = GetThreadSelectorEntry (thread, sel, &info); | |
1081 | if (ret) | |
c1748f97 PM |
1082 | { |
1083 | int base, limit; | |
d50a0ce2 | 1084 | printf_filtered ("0x%03x: ", (unsigned) sel); |
c1748f97 | 1085 | if (!info.HighWord.Bits.Pres) |
baa93fa6 CF |
1086 | { |
1087 | puts_filtered ("Segment not present\n"); | |
1088 | return 0; | |
1089 | } | |
c1748f97 PM |
1090 | base = (info.HighWord.Bits.BaseHi << 24) + |
1091 | (info.HighWord.Bits.BaseMid << 16) | |
1092 | + info.BaseLow; | |
1093 | limit = (info.HighWord.Bits.LimitHi << 16) + info.LimitLow; | |
1094 | if (info.HighWord.Bits.Granularity) | |
caad7706 | 1095 | limit = (limit << 12) | 0xfff; |
c1748f97 PM |
1096 | printf_filtered ("base=0x%08x limit=0x%08x", base, limit); |
1097 | if (info.HighWord.Bits.Default_Big) | |
baa93fa6 | 1098 | puts_filtered(" 32-bit "); |
c1748f97 | 1099 | else |
baa93fa6 | 1100 | puts_filtered(" 16-bit "); |
c1748f97 PM |
1101 | switch ((info.HighWord.Bits.Type & 0xf) >> 1) |
1102 | { | |
1103 | case 0: | |
baa93fa6 CF |
1104 | puts_filtered ("Data (Read-Only, Exp-up"); |
1105 | break; | |
c1748f97 | 1106 | case 1: |
baa93fa6 CF |
1107 | puts_filtered ("Data (Read/Write, Exp-up"); |
1108 | break; | |
c1748f97 | 1109 | case 2: |
baa93fa6 CF |
1110 | puts_filtered ("Unused segment ("); |
1111 | break; | |
c1748f97 | 1112 | case 3: |
baa93fa6 CF |
1113 | puts_filtered ("Data (Read/Write, Exp-down"); |
1114 | break; | |
c1748f97 | 1115 | case 4: |
baa93fa6 CF |
1116 | puts_filtered ("Code (Exec-Only, N.Conf"); |
1117 | break; | |
c1748f97 | 1118 | case 5: |
baa93fa6 | 1119 | puts_filtered ("Code (Exec/Read, N.Conf"); |
c1748f97 PM |
1120 | break; |
1121 | case 6: | |
baa93fa6 | 1122 | puts_filtered ("Code (Exec-Only, Conf"); |
c1748f97 PM |
1123 | break; |
1124 | case 7: | |
baa93fa6 | 1125 | puts_filtered ("Code (Exec/Read, Conf"); |
c1748f97 PM |
1126 | break; |
1127 | default: | |
e432ccf1 JT |
1128 | printf_filtered ("Unknown type 0x%lx", |
1129 | (unsigned long) info.HighWord.Bits.Type); | |
c1748f97 PM |
1130 | } |
1131 | if ((info.HighWord.Bits.Type & 0x1) == 0) | |
baa93fa6 | 1132 | puts_filtered(", N.Acc"); |
c1748f97 PM |
1133 | puts_filtered (")\n"); |
1134 | if ((info.HighWord.Bits.Type & 0x10) == 0) | |
1135 | puts_filtered("System selector "); | |
e432ccf1 JT |
1136 | printf_filtered ("Priviledge level = %ld. ", |
1137 | (unsigned long) info.HighWord.Bits.Dpl); | |
c1748f97 | 1138 | if (info.HighWord.Bits.Granularity) |
baa93fa6 | 1139 | puts_filtered ("Page granular.\n"); |
c1748f97 PM |
1140 | else |
1141 | puts_filtered ("Byte granular.\n"); | |
1142 | return 1; | |
1143 | } | |
1144 | else | |
1145 | { | |
5572ce1f PM |
1146 | DWORD err = GetLastError (); |
1147 | if (err == ERROR_NOT_SUPPORTED) | |
1148 | printf_filtered ("Function not supported\n"); | |
1149 | else | |
d50a0ce2 | 1150 | printf_filtered ("Invalid selector 0x%x.\n", (unsigned) sel); |
c1748f97 PM |
1151 | return 0; |
1152 | } | |
1153 | } | |
1154 | ||
1155 | static void | |
5fed81ff | 1156 | display_selectors (const char * args, int from_tty) |
c1748f97 | 1157 | { |
3c76026d | 1158 | if (!current_windows_thread) |
c1748f97 PM |
1159 | { |
1160 | puts_filtered ("Impossible to display selectors now.\n"); | |
1161 | return; | |
1162 | } | |
1163 | if (!args) | |
1164 | { | |
46f9f931 HD |
1165 | #ifdef __x86_64__ |
1166 | if (wow64_process) | |
1167 | { | |
1168 | puts_filtered ("Selector $cs\n"); | |
3c76026d TT |
1169 | display_selector (current_windows_thread->h, |
1170 | current_windows_thread->wow64_context.SegCs); | |
46f9f931 | 1171 | puts_filtered ("Selector $ds\n"); |
3c76026d TT |
1172 | display_selector (current_windows_thread->h, |
1173 | current_windows_thread->wow64_context.SegDs); | |
46f9f931 | 1174 | puts_filtered ("Selector $es\n"); |
3c76026d TT |
1175 | display_selector (current_windows_thread->h, |
1176 | current_windows_thread->wow64_context.SegEs); | |
46f9f931 | 1177 | puts_filtered ("Selector $ss\n"); |
3c76026d TT |
1178 | display_selector (current_windows_thread->h, |
1179 | current_windows_thread->wow64_context.SegSs); | |
46f9f931 | 1180 | puts_filtered ("Selector $fs\n"); |
3c76026d TT |
1181 | display_selector (current_windows_thread->h, |
1182 | current_windows_thread->wow64_context.SegFs); | |
46f9f931 | 1183 | puts_filtered ("Selector $gs\n"); |
3c76026d TT |
1184 | display_selector (current_windows_thread->h, |
1185 | current_windows_thread->wow64_context.SegGs); | |
46f9f931 HD |
1186 | } |
1187 | else | |
1188 | #endif | |
1189 | { | |
1190 | puts_filtered ("Selector $cs\n"); | |
3c76026d TT |
1191 | display_selector (current_windows_thread->h, |
1192 | current_windows_thread->context.SegCs); | |
46f9f931 | 1193 | puts_filtered ("Selector $ds\n"); |
3c76026d TT |
1194 | display_selector (current_windows_thread->h, |
1195 | current_windows_thread->context.SegDs); | |
46f9f931 | 1196 | puts_filtered ("Selector $es\n"); |
3c76026d TT |
1197 | display_selector (current_windows_thread->h, |
1198 | current_windows_thread->context.SegEs); | |
46f9f931 | 1199 | puts_filtered ("Selector $ss\n"); |
3c76026d TT |
1200 | display_selector (current_windows_thread->h, |
1201 | current_windows_thread->context.SegSs); | |
46f9f931 | 1202 | puts_filtered ("Selector $fs\n"); |
3c76026d TT |
1203 | display_selector (current_windows_thread->h, |
1204 | current_windows_thread->context.SegFs); | |
46f9f931 | 1205 | puts_filtered ("Selector $gs\n"); |
3c76026d TT |
1206 | display_selector (current_windows_thread->h, |
1207 | current_windows_thread->context.SegGs); | |
46f9f931 | 1208 | } |
c1748f97 PM |
1209 | } |
1210 | else | |
1211 | { | |
1212 | int sel; | |
1213 | sel = parse_and_eval_long (args); | |
1214 | printf_filtered ("Selector \"%s\"\n",args); | |
3c76026d | 1215 | display_selector (current_windows_thread->h, sel); |
c1748f97 PM |
1216 | } |
1217 | } | |
1218 | ||
7393af7c | 1219 | #define DEBUG_EXCEPTION_SIMPLE(x) if (debug_exceptions) \ |
a74ce742 PM |
1220 | printf_unfiltered ("gdb: Target exception %s at %s\n", x, \ |
1221 | host_address_to_string (\ | |
1222 | current_event.u.Exception.ExceptionRecord.ExceptionAddress)) | |
7393af7c | 1223 | |
24cdb46e | 1224 | static handle_exception_result |
450005e7 | 1225 | handle_exception (struct target_waitstatus *ourstatus) |
24e60978 | 1226 | { |
24cdb46e РИ |
1227 | EXCEPTION_RECORD *rec = ¤t_event.u.Exception.ExceptionRecord; |
1228 | DWORD code = rec->ExceptionCode; | |
1229 | handle_exception_result result = HANDLE_EXCEPTION_HANDLED; | |
3cee93ac | 1230 | |
7928d571 HD |
1231 | memcpy (&siginfo_er, rec, sizeof siginfo_er); |
1232 | ||
29fe111d | 1233 | ourstatus->kind = TARGET_WAITKIND_STOPPED; |
8a892701 | 1234 | |
581e13c1 | 1235 | /* Record the context of the current thread. */ |
28688adf TT |
1236 | thread_rec (ptid_t (current_event.dwProcessId, current_event.dwThreadId, 0), |
1237 | DONT_SUSPEND); | |
24e60978 | 1238 | |
29fe111d | 1239 | switch (code) |
24e60978 | 1240 | { |
1ef980b9 | 1241 | case EXCEPTION_ACCESS_VIOLATION: |
7393af7c | 1242 | DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_ACCESS_VIOLATION"); |
a493e3e2 | 1243 | ourstatus->value.sig = GDB_SIGNAL_SEGV; |
10325bc5 | 1244 | #ifdef __CYGWIN__ |
8da8e0b3 | 1245 | { |
581e13c1 MS |
1246 | /* See if the access violation happened within the cygwin DLL |
1247 | itself. Cygwin uses a kind of exception handling to deal | |
1248 | with passed-in invalid addresses. gdb should not treat | |
1249 | these as real SEGVs since they will be silently handled by | |
1250 | cygwin. A real SEGV will (theoretically) be caught by | |
1251 | cygwin later in the process and will be sent as a | |
1252 | cygwin-specific-signal. So, ignore SEGVs if they show up | |
1253 | within the text segment of the DLL itself. */ | |
2c02bd72 | 1254 | const char *fn; |
24cdb46e | 1255 | CORE_ADDR addr = (CORE_ADDR) (uintptr_t) rec->ExceptionAddress; |
581e13c1 MS |
1256 | |
1257 | if ((!cygwin_exceptions && (addr >= cygwin_load_start | |
1258 | && addr < cygwin_load_end)) | |
a244bdca | 1259 | || (find_pc_partial_function (addr, &fn, NULL, NULL) |
61012eef | 1260 | && startswith (fn, "KERNEL32!IsBad"))) |
24cdb46e | 1261 | return HANDLE_EXCEPTION_UNHANDLED; |
8da8e0b3 | 1262 | } |
10325bc5 | 1263 | #endif |
7393af7c PM |
1264 | break; |
1265 | case STATUS_STACK_OVERFLOW: | |
1266 | DEBUG_EXCEPTION_SIMPLE ("STATUS_STACK_OVERFLOW"); | |
a493e3e2 | 1267 | ourstatus->value.sig = GDB_SIGNAL_SEGV; |
7393af7c PM |
1268 | break; |
1269 | case STATUS_FLOAT_DENORMAL_OPERAND: | |
1270 | DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_DENORMAL_OPERAND"); | |
a493e3e2 | 1271 | ourstatus->value.sig = GDB_SIGNAL_FPE; |
7393af7c PM |
1272 | break; |
1273 | case EXCEPTION_ARRAY_BOUNDS_EXCEEDED: | |
1274 | DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_ARRAY_BOUNDS_EXCEEDED"); | |
a493e3e2 | 1275 | ourstatus->value.sig = GDB_SIGNAL_FPE; |
7393af7c PM |
1276 | break; |
1277 | case STATUS_FLOAT_INEXACT_RESULT: | |
1278 | DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_INEXACT_RESULT"); | |
a493e3e2 | 1279 | ourstatus->value.sig = GDB_SIGNAL_FPE; |
7393af7c PM |
1280 | break; |
1281 | case STATUS_FLOAT_INVALID_OPERATION: | |
1282 | DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_INVALID_OPERATION"); | |
a493e3e2 | 1283 | ourstatus->value.sig = GDB_SIGNAL_FPE; |
7393af7c PM |
1284 | break; |
1285 | case STATUS_FLOAT_OVERFLOW: | |
1286 | DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_OVERFLOW"); | |
a493e3e2 | 1287 | ourstatus->value.sig = GDB_SIGNAL_FPE; |
7393af7c PM |
1288 | break; |
1289 | case STATUS_FLOAT_STACK_CHECK: | |
1290 | DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_STACK_CHECK"); | |
a493e3e2 | 1291 | ourstatus->value.sig = GDB_SIGNAL_FPE; |
1ef980b9 | 1292 | break; |
3b7c8b74 | 1293 | case STATUS_FLOAT_UNDERFLOW: |
7393af7c | 1294 | DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_UNDERFLOW"); |
a493e3e2 | 1295 | ourstatus->value.sig = GDB_SIGNAL_FPE; |
7393af7c | 1296 | break; |
3b7c8b74 | 1297 | case STATUS_FLOAT_DIVIDE_BY_ZERO: |
7393af7c | 1298 | DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_DIVIDE_BY_ZERO"); |
a493e3e2 | 1299 | ourstatus->value.sig = GDB_SIGNAL_FPE; |
7393af7c | 1300 | break; |
3b7c8b74 | 1301 | case STATUS_INTEGER_DIVIDE_BY_ZERO: |
7393af7c | 1302 | DEBUG_EXCEPTION_SIMPLE ("STATUS_INTEGER_DIVIDE_BY_ZERO"); |
a493e3e2 | 1303 | ourstatus->value.sig = GDB_SIGNAL_FPE; |
3b7c8b74 | 1304 | break; |
7393af7c PM |
1305 | case STATUS_INTEGER_OVERFLOW: |
1306 | DEBUG_EXCEPTION_SIMPLE ("STATUS_INTEGER_OVERFLOW"); | |
a493e3e2 | 1307 | ourstatus->value.sig = GDB_SIGNAL_FPE; |
1ef980b9 SC |
1308 | break; |
1309 | case EXCEPTION_BREAKPOINT: | |
46f9f931 HD |
1310 | #ifdef __x86_64__ |
1311 | if (ignore_first_breakpoint) | |
1312 | { | |
1313 | /* For WOW64 processes, there are always 2 breakpoint exceptions | |
1314 | on startup, first a BREAKPOINT for the 64bit ntdll.dll, | |
1315 | then a WX86_BREAKPOINT for the 32bit ntdll.dll. | |
1316 | Here we only care about the WX86_BREAKPOINT's. */ | |
1317 | ourstatus->kind = TARGET_WAITKIND_SPURIOUS; | |
1318 | ignore_first_breakpoint = false; | |
1319 | } | |
1320 | #endif | |
1321 | /* FALLTHROUGH */ | |
1322 | case STATUS_WX86_BREAKPOINT: | |
7393af7c | 1323 | DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_BREAKPOINT"); |
a493e3e2 | 1324 | ourstatus->value.sig = GDB_SIGNAL_TRAP; |
1ef980b9 SC |
1325 | break; |
1326 | case DBG_CONTROL_C: | |
7393af7c | 1327 | DEBUG_EXCEPTION_SIMPLE ("DBG_CONTROL_C"); |
a493e3e2 | 1328 | ourstatus->value.sig = GDB_SIGNAL_INT; |
5b421780 PM |
1329 | break; |
1330 | case DBG_CONTROL_BREAK: | |
7393af7c | 1331 | DEBUG_EXCEPTION_SIMPLE ("DBG_CONTROL_BREAK"); |
a493e3e2 | 1332 | ourstatus->value.sig = GDB_SIGNAL_INT; |
1ef980b9 SC |
1333 | break; |
1334 | case EXCEPTION_SINGLE_STEP: | |
46f9f931 | 1335 | case STATUS_WX86_SINGLE_STEP: |
7393af7c | 1336 | DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_SINGLE_STEP"); |
a493e3e2 | 1337 | ourstatus->value.sig = GDB_SIGNAL_TRAP; |
1ef980b9 | 1338 | break; |
8227c82d | 1339 | case EXCEPTION_ILLEGAL_INSTRUCTION: |
7393af7c | 1340 | DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_ILLEGAL_INSTRUCTION"); |
a493e3e2 | 1341 | ourstatus->value.sig = GDB_SIGNAL_ILL; |
7393af7c PM |
1342 | break; |
1343 | case EXCEPTION_PRIV_INSTRUCTION: | |
1344 | DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_PRIV_INSTRUCTION"); | |
a493e3e2 | 1345 | ourstatus->value.sig = GDB_SIGNAL_ILL; |
7393af7c PM |
1346 | break; |
1347 | case EXCEPTION_NONCONTINUABLE_EXCEPTION: | |
1348 | DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_NONCONTINUABLE_EXCEPTION"); | |
a493e3e2 | 1349 | ourstatus->value.sig = GDB_SIGNAL_ILL; |
8227c82d | 1350 | break; |
24cdb46e РИ |
1351 | case MS_VC_EXCEPTION: |
1352 | if (rec->NumberParameters >= 3 | |
1353 | && (rec->ExceptionInformation[0] & 0xffffffff) == 0x1000) | |
1354 | { | |
1355 | DWORD named_thread_id; | |
1356 | windows_thread_info *named_thread; | |
1357 | CORE_ADDR thread_name_target; | |
1358 | ||
1359 | DEBUG_EXCEPTION_SIMPLE ("MS_VC_EXCEPTION"); | |
1360 | ||
1361 | thread_name_target = rec->ExceptionInformation[1]; | |
1362 | named_thread_id = (DWORD) (0xffffffff & rec->ExceptionInformation[2]); | |
1363 | ||
1364 | if (named_thread_id == (DWORD) -1) | |
1365 | named_thread_id = current_event.dwThreadId; | |
1366 | ||
28688adf TT |
1367 | named_thread = thread_rec (ptid_t (current_event.dwProcessId, |
1368 | named_thread_id, 0), | |
1369 | DONT_INVALIDATE_CONTEXT); | |
24cdb46e РИ |
1370 | if (named_thread != NULL) |
1371 | { | |
1372 | int thread_name_len; | |
e83e4e24 | 1373 | gdb::unique_xmalloc_ptr<char> thread_name; |
24cdb46e РИ |
1374 | |
1375 | thread_name_len = target_read_string (thread_name_target, | |
1376 | &thread_name, 1025, NULL); | |
1377 | if (thread_name_len > 0) | |
1378 | { | |
e83e4e24 | 1379 | thread_name.get ()[thread_name_len - 1] = '\0'; |
2950fdf7 | 1380 | named_thread->name = std::move (thread_name); |
24cdb46e | 1381 | } |
24cdb46e РИ |
1382 | } |
1383 | ourstatus->value.sig = GDB_SIGNAL_TRAP; | |
1384 | result = HANDLE_EXCEPTION_IGNORED; | |
1385 | break; | |
1386 | } | |
e7ec8713 TT |
1387 | /* treat improperly formed exception as unknown */ |
1388 | /* FALLTHROUGH */ | |
1ef980b9 | 1389 | default: |
581e13c1 | 1390 | /* Treat unhandled first chance exceptions specially. */ |
02e423b9 | 1391 | if (current_event.u.Exception.dwFirstChance) |
24cdb46e | 1392 | return HANDLE_EXCEPTION_UNHANDLED; |
d50a0ce2 CV |
1393 | printf_unfiltered ("gdb: unknown target exception 0x%08x at %s\n", |
1394 | (unsigned) current_event.u.Exception.ExceptionRecord.ExceptionCode, | |
a74ce742 PM |
1395 | host_address_to_string ( |
1396 | current_event.u.Exception.ExceptionRecord.ExceptionAddress)); | |
a493e3e2 | 1397 | ourstatus->value.sig = GDB_SIGNAL_UNKNOWN; |
1ef980b9 | 1398 | break; |
24e60978 | 1399 | } |
24e60978 | 1400 | exception_count++; |
7393af7c | 1401 | last_sig = ourstatus->value.sig; |
24cdb46e | 1402 | return result; |
24e60978 SC |
1403 | } |
1404 | ||
17617f2d EZ |
1405 | /* Resume thread specified by ID, or all artificially suspended |
1406 | threads, if we are continuing execution. KILLED non-zero means we | |
1407 | have killed the inferior, so we should ignore weird errors due to | |
1408 | threads shutting down. */ | |
3cee93ac | 1409 | static BOOL |
17617f2d | 1410 | windows_continue (DWORD continue_status, int id, int killed) |
3cee93ac | 1411 | { |
3cee93ac CF |
1412 | BOOL res; |
1413 | ||
0a4afda3 TT |
1414 | desired_stop_thread_id = id; |
1415 | ||
1416 | /* If there are pending stops, and we might plausibly hit one of | |
1417 | them, we don't want to actually continue the inferior -- we just | |
1418 | want to report the stop. In this case, we just pretend to | |
1419 | continue. See the comment by the definition of "pending_stops" | |
1420 | for details on why this is needed. */ | |
1421 | for (const auto &item : pending_stops) | |
1422 | { | |
1423 | if (desired_stop_thread_id == -1 | |
1424 | || desired_stop_thread_id == item.thread_id) | |
1425 | { | |
1426 | DEBUG_EVENTS (("windows_continue - pending stop anticipated, " | |
1427 | "desired=0x%x, item=0x%x\n", | |
1428 | desired_stop_thread_id, item.thread_id)); | |
1429 | return TRUE; | |
1430 | } | |
1431 | } | |
1432 | ||
0c3d84be | 1433 | DEBUG_EVENTS (("ContinueDebugEvent (cpid=%d, ctid=0x%x, %s);\n", |
0a4afda3 TT |
1434 | (unsigned) last_wait_event.dwProcessId, |
1435 | (unsigned) last_wait_event.dwThreadId, | |
dfe7f3ac | 1436 | continue_status == DBG_CONTINUE ? |
7393af7c | 1437 | "DBG_CONTINUE" : "DBG_EXCEPTION_NOT_HANDLED")); |
6537bb24 | 1438 | |
93366324 | 1439 | for (windows_thread_info *th : thread_list) |
0a4afda3 | 1440 | if (id == -1 || id == (int) th->tid) |
6537bb24 | 1441 | { |
0a4afda3 TT |
1442 | if (!th->suspended) |
1443 | continue; | |
46f9f931 HD |
1444 | #ifdef __x86_64__ |
1445 | if (wow64_process) | |
6537bb24 | 1446 | { |
46f9f931 HD |
1447 | if (debug_registers_changed) |
1448 | { | |
1449 | th->wow64_context.ContextFlags |= CONTEXT_DEBUG_REGISTERS; | |
1450 | th->wow64_context.Dr0 = dr[0]; | |
1451 | th->wow64_context.Dr1 = dr[1]; | |
1452 | th->wow64_context.Dr2 = dr[2]; | |
1453 | th->wow64_context.Dr3 = dr[3]; | |
1454 | th->wow64_context.Dr6 = DR6_CLEAR_VALUE; | |
1455 | th->wow64_context.Dr7 = dr[7]; | |
1456 | } | |
1457 | if (th->wow64_context.ContextFlags) | |
1458 | { | |
1459 | DWORD ec = 0; | |
1460 | ||
1461 | if (GetExitCodeThread (th->h, &ec) | |
1462 | && ec == STILL_ACTIVE) | |
1463 | { | |
1464 | BOOL status = Wow64SetThreadContext (th->h, | |
1465 | &th->wow64_context); | |
1466 | ||
1467 | if (!killed) | |
1468 | CHECK (status); | |
1469 | } | |
1470 | th->wow64_context.ContextFlags = 0; | |
1471 | } | |
6537bb24 | 1472 | } |
46f9f931 HD |
1473 | else |
1474 | #endif | |
6537bb24 | 1475 | { |
46f9f931 | 1476 | if (debug_registers_changed) |
17617f2d | 1477 | { |
46f9f931 HD |
1478 | th->context.ContextFlags |= CONTEXT_DEBUG_REGISTERS; |
1479 | th->context.Dr0 = dr[0]; | |
1480 | th->context.Dr1 = dr[1]; | |
1481 | th->context.Dr2 = dr[2]; | |
1482 | th->context.Dr3 = dr[3]; | |
1483 | th->context.Dr6 = DR6_CLEAR_VALUE; | |
1484 | th->context.Dr7 = dr[7]; | |
1485 | } | |
1486 | if (th->context.ContextFlags) | |
1487 | { | |
1488 | DWORD ec = 0; | |
1489 | ||
1490 | if (GetExitCodeThread (th->h, &ec) | |
1491 | && ec == STILL_ACTIVE) | |
1492 | { | |
1493 | BOOL status = SetThreadContext (th->h, &th->context); | |
17617f2d | 1494 | |
46f9f931 HD |
1495 | if (!killed) |
1496 | CHECK (status); | |
1497 | } | |
1498 | th->context.ContextFlags = 0; | |
17617f2d | 1499 | } |
6537bb24 | 1500 | } |
98a03287 | 1501 | th->resume (); |
6537bb24 | 1502 | } |
0a4afda3 TT |
1503 | else |
1504 | { | |
1505 | /* When single-stepping a specific thread, other threads must | |
1506 | be suspended. */ | |
1507 | th->suspend (); | |
1508 | } | |
6537bb24 | 1509 | |
0a4afda3 TT |
1510 | res = ContinueDebugEvent (last_wait_event.dwProcessId, |
1511 | last_wait_event.dwThreadId, | |
0714f9bf | 1512 | continue_status); |
3cee93ac | 1513 | |
68ffc902 JT |
1514 | if (!res) |
1515 | error (_("Failed to resume program execution" | |
1516 | " (ContinueDebugEvent failed, error %u)"), | |
1517 | (unsigned int) GetLastError ()); | |
1518 | ||
fa4ba8da | 1519 | debug_registers_changed = 0; |
3cee93ac CF |
1520 | return res; |
1521 | } | |
1522 | ||
d6dc8049 CF |
1523 | /* Called in pathological case where Windows fails to send a |
1524 | CREATE_PROCESS_DEBUG_EVENT after an attach. */ | |
3ee6f623 | 1525 | static DWORD |
5439edaa | 1526 | fake_create_process (void) |
3ade5333 CF |
1527 | { |
1528 | current_process_handle = OpenProcess (PROCESS_ALL_ACCESS, FALSE, | |
1529 | current_event.dwProcessId); | |
bf25528d CF |
1530 | if (current_process_handle != NULL) |
1531 | open_process_used = 1; | |
1532 | else | |
1533 | { | |
d50a0ce2 CV |
1534 | error (_("OpenProcess call failed, GetLastError = %u"), |
1535 | (unsigned) GetLastError ()); | |
bf25528d CF |
1536 | /* We can not debug anything in that case. */ |
1537 | } | |
3c76026d | 1538 | current_windows_thread |
7c7411bc TT |
1539 | = windows_add_thread (ptid_t (current_event.dwProcessId, |
1540 | current_event.dwThreadId, 0), | |
c559d709 JB |
1541 | current_event.u.CreateThread.hThread, |
1542 | current_event.u.CreateThread.lpThreadLocalBase, | |
1543 | true /* main_thread_p */); | |
ab4ee614 | 1544 | return current_event.dwThreadId; |
3ade5333 CF |
1545 | } |
1546 | ||
f6ac5f3d PA |
1547 | void |
1548 | windows_nat_target::resume (ptid_t ptid, int step, enum gdb_signal sig) | |
a244bdca | 1549 | { |
876d1cd7 | 1550 | windows_thread_info *th; |
a244bdca CF |
1551 | DWORD continue_status = DBG_CONTINUE; |
1552 | ||
2dc38344 | 1553 | /* A specific PTID means `step only this thread id'. */ |
d7e15655 | 1554 | int resume_all = ptid == minus_one_ptid; |
2dc38344 PA |
1555 | |
1556 | /* If we're continuing all threads, it's the current inferior that | |
1557 | should be handled specially. */ | |
1558 | if (resume_all) | |
1559 | ptid = inferior_ptid; | |
a244bdca | 1560 | |
a493e3e2 | 1561 | if (sig != GDB_SIGNAL_0) |
a244bdca CF |
1562 | { |
1563 | if (current_event.dwDebugEventCode != EXCEPTION_DEBUG_EVENT) | |
1564 | { | |
1565 | DEBUG_EXCEPT(("Cannot continue with signal %d here.\n",sig)); | |
1566 | } | |
1567 | else if (sig == last_sig) | |
1568 | continue_status = DBG_EXCEPTION_NOT_HANDLED; | |
1569 | else | |
1570 | #if 0 | |
1571 | /* This code does not seem to work, because | |
1572 | the kernel does probably not consider changes in the ExceptionRecord | |
1573 | structure when passing the exception to the inferior. | |
1574 | Note that this seems possible in the exception handler itself. */ | |
1575 | { | |
73c13fe6 TT |
1576 | for (const xlate_exception &x : xlate) |
1577 | if (x.us == sig) | |
a244bdca | 1578 | { |
581e13c1 | 1579 | current_event.u.Exception.ExceptionRecord.ExceptionCode |
73c13fe6 | 1580 | = x.them; |
a244bdca CF |
1581 | continue_status = DBG_EXCEPTION_NOT_HANDLED; |
1582 | break; | |
1583 | } | |
1584 | if (continue_status == DBG_CONTINUE) | |
1585 | { | |
1586 | DEBUG_EXCEPT(("Cannot continue with signal %d.\n",sig)); | |
1587 | } | |
1588 | } | |
1589 | #endif | |
23942819 | 1590 | DEBUG_EXCEPT(("Can only continue with received signal %d.\n", |
a244bdca CF |
1591 | last_sig)); |
1592 | } | |
1593 | ||
a493e3e2 | 1594 | last_sig = GDB_SIGNAL_0; |
a244bdca | 1595 | |
55dfc88f | 1596 | DEBUG_EXEC (("gdb: windows_resume (pid=%d, tid=0x%x, step=%d, sig=%d);\n", |
7c7411bc | 1597 | ptid.pid (), (unsigned) ptid.lwp (), step, sig)); |
a244bdca | 1598 | |
581e13c1 | 1599 | /* Get context for currently selected thread. */ |
28688adf | 1600 | th = thread_rec (inferior_ptid, DONT_INVALIDATE_CONTEXT); |
a244bdca CF |
1601 | if (th) |
1602 | { | |
46f9f931 HD |
1603 | #ifdef __x86_64__ |
1604 | if (wow64_process) | |
a244bdca | 1605 | { |
46f9f931 HD |
1606 | if (step) |
1607 | { | |
1608 | /* Single step by setting t bit. */ | |
1609 | struct regcache *regcache = get_current_regcache (); | |
1610 | struct gdbarch *gdbarch = regcache->arch (); | |
1611 | fetch_registers (regcache, gdbarch_ps_regnum (gdbarch)); | |
1612 | th->wow64_context.EFlags |= FLAG_TRACE_BIT; | |
1613 | } | |
a244bdca | 1614 | |
46f9f931 HD |
1615 | if (th->wow64_context.ContextFlags) |
1616 | { | |
1617 | if (debug_registers_changed) | |
1618 | { | |
1619 | th->wow64_context.Dr0 = dr[0]; | |
1620 | th->wow64_context.Dr1 = dr[1]; | |
1621 | th->wow64_context.Dr2 = dr[2]; | |
1622 | th->wow64_context.Dr3 = dr[3]; | |
1623 | th->wow64_context.Dr6 = DR6_CLEAR_VALUE; | |
1624 | th->wow64_context.Dr7 = dr[7]; | |
1625 | } | |
1626 | CHECK (Wow64SetThreadContext (th->h, &th->wow64_context)); | |
1627 | th->wow64_context.ContextFlags = 0; | |
1628 | } | |
1629 | } | |
1630 | else | |
1631 | #endif | |
a244bdca | 1632 | { |
46f9f931 | 1633 | if (step) |
a244bdca | 1634 | { |
46f9f931 HD |
1635 | /* Single step by setting t bit. */ |
1636 | struct regcache *regcache = get_current_regcache (); | |
1637 | struct gdbarch *gdbarch = regcache->arch (); | |
1638 | fetch_registers (regcache, gdbarch_ps_regnum (gdbarch)); | |
1639 | th->context.EFlags |= FLAG_TRACE_BIT; | |
1640 | } | |
1641 | ||
1642 | if (th->context.ContextFlags) | |
1643 | { | |
1644 | if (debug_registers_changed) | |
1645 | { | |
1646 | th->context.Dr0 = dr[0]; | |
1647 | th->context.Dr1 = dr[1]; | |
1648 | th->context.Dr2 = dr[2]; | |
1649 | th->context.Dr3 = dr[3]; | |
1650 | th->context.Dr6 = DR6_CLEAR_VALUE; | |
1651 | th->context.Dr7 = dr[7]; | |
1652 | } | |
1653 | CHECK (SetThreadContext (th->h, &th->context)); | |
1654 | th->context.ContextFlags = 0; | |
a244bdca | 1655 | } |
a244bdca CF |
1656 | } |
1657 | } | |
1658 | ||
1659 | /* Allow continuing with the same signal that interrupted us. | |
581e13c1 | 1660 | Otherwise complain. */ |
a244bdca | 1661 | |
2dc38344 | 1662 | if (resume_all) |
17617f2d | 1663 | windows_continue (continue_status, -1, 0); |
2dc38344 | 1664 | else |
7c7411bc | 1665 | windows_continue (continue_status, ptid.lwp (), 0); |
a244bdca CF |
1666 | } |
1667 | ||
695de547 CF |
1668 | /* Ctrl-C handler used when the inferior is not run in the same console. The |
1669 | handler is in charge of interrupting the inferior using DebugBreakProcess. | |
1670 | Note that this function is not available prior to Windows XP. In this case | |
1671 | we emit a warning. */ | |
d603d4b3 | 1672 | static BOOL WINAPI |
695de547 CF |
1673 | ctrl_c_handler (DWORD event_type) |
1674 | { | |
1675 | const int attach_flag = current_inferior ()->attach_flag; | |
1676 | ||
bb0613a5 PM |
1677 | /* Only handle Ctrl-C and Ctrl-Break events. Ignore others. */ |
1678 | if (event_type != CTRL_C_EVENT && event_type != CTRL_BREAK_EVENT) | |
695de547 CF |
1679 | return FALSE; |
1680 | ||
1681 | /* If the inferior and the debugger share the same console, do nothing as | |
1682 | the inferior has also received the Ctrl-C event. */ | |
1683 | if (!new_console && !attach_flag) | |
1684 | return TRUE; | |
1685 | ||
1686 | if (!DebugBreakProcess (current_process_handle)) | |
581e13c1 MS |
1687 | warning (_("Could not interrupt program. " |
1688 | "Press Ctrl-c in the program console.")); | |
695de547 CF |
1689 | |
1690 | /* Return true to tell that Ctrl-C has been handled. */ | |
1691 | return TRUE; | |
1692 | } | |
1693 | ||
0a4afda3 TT |
1694 | /* A wrapper for WaitForDebugEvent that sets "last_wait_event" |
1695 | appropriately. */ | |
1696 | static BOOL | |
1697 | wait_for_debug_event (DEBUG_EVENT *event, DWORD timeout) | |
1698 | { | |
1699 | BOOL result = WaitForDebugEvent (event, timeout); | |
1700 | if (result) | |
1701 | last_wait_event = *event; | |
1702 | return result; | |
1703 | } | |
1704 | ||
e6ad66bd JT |
1705 | /* Get the next event from the child. Returns a non-zero thread id if the event |
1706 | requires handling by WFI (or whatever). */ | |
5b6d1e4f PA |
1707 | |
1708 | int | |
1709 | windows_nat_target::get_windows_debug_event (int pid, | |
1710 | struct target_waitstatus *ourstatus) | |
1e37c281 JM |
1711 | { |
1712 | BOOL debug_event; | |
8a892701 | 1713 | DWORD continue_status, event_code; |
876d1cd7 | 1714 | windows_thread_info *th; |
e9534bd2 | 1715 | static windows_thread_info dummy_thread_info (0, 0, 0); |
e6ad66bd | 1716 | DWORD thread_id = 0; |
1e37c281 | 1717 | |
0a4afda3 TT |
1718 | /* If there is a relevant pending stop, report it now. See the |
1719 | comment by the definition of "pending_stops" for details on why | |
1720 | this is needed. */ | |
1721 | for (auto iter = pending_stops.begin (); | |
1722 | iter != pending_stops.end (); | |
1723 | ++iter) | |
1724 | { | |
1725 | if (desired_stop_thread_id == -1 | |
1726 | || desired_stop_thread_id == iter->thread_id) | |
1727 | { | |
1728 | thread_id = iter->thread_id; | |
1729 | *ourstatus = iter->status; | |
1730 | current_event = iter->event; | |
1731 | ||
1732 | inferior_ptid = ptid_t (current_event.dwProcessId, thread_id, 0); | |
3c76026d TT |
1733 | current_windows_thread = thread_rec (inferior_ptid, |
1734 | INVALIDATE_CONTEXT); | |
1735 | current_windows_thread->reload_context = 1; | |
0a4afda3 TT |
1736 | |
1737 | DEBUG_EVENTS (("get_windows_debug_event - " | |
1738 | "pending stop found in 0x%x (desired=0x%x)\n", | |
1739 | thread_id, desired_stop_thread_id)); | |
1740 | ||
1741 | pending_stops.erase (iter); | |
1742 | return thread_id; | |
1743 | } | |
1744 | } | |
1745 | ||
a493e3e2 | 1746 | last_sig = GDB_SIGNAL_0; |
9d3789f7 | 1747 | |
0a4afda3 | 1748 | if (!(debug_event = wait_for_debug_event (¤t_event, 1000))) |
29fe111d | 1749 | goto out; |
1e37c281 JM |
1750 | |
1751 | event_count++; | |
1752 | continue_status = DBG_CONTINUE; | |
1e37c281 | 1753 | |
8a892701 | 1754 | event_code = current_event.dwDebugEventCode; |
450005e7 | 1755 | ourstatus->kind = TARGET_WAITKIND_SPURIOUS; |
87a45c96 | 1756 | th = NULL; |
a244bdca | 1757 | have_saved_context = 0; |
8a892701 CF |
1758 | |
1759 | switch (event_code) | |
1e37c281 JM |
1760 | { |
1761 | case CREATE_THREAD_DEBUG_EVENT: | |
0c3d84be | 1762 | DEBUG_EVENTS (("gdb: kernel event for pid=%u tid=0x%x code=%s)\n", |
8a892701 CF |
1763 | (unsigned) current_event.dwProcessId, |
1764 | (unsigned) current_event.dwThreadId, | |
1765 | "CREATE_THREAD_DEBUG_EVENT")); | |
dfe7f3ac | 1766 | if (saw_create != 1) |
3ade5333 | 1767 | { |
5b6d1e4f | 1768 | inferior *inf = find_inferior_pid (this, current_event.dwProcessId); |
181e7f93 | 1769 | if (!saw_create && inf->attach_flag) |
3ade5333 | 1770 | { |
d6dc8049 CF |
1771 | /* Kludge around a Windows bug where first event is a create |
1772 | thread event. Caused when attached process does not have | |
581e13c1 | 1773 | a main thread. */ |
e6ad66bd JT |
1774 | thread_id = fake_create_process (); |
1775 | if (thread_id) | |
181e7f93 | 1776 | saw_create++; |
3ade5333 CF |
1777 | } |
1778 | break; | |
1779 | } | |
581e13c1 | 1780 | /* Record the existence of this thread. */ |
e6ad66bd | 1781 | thread_id = current_event.dwThreadId; |
c559d709 | 1782 | th = windows_add_thread |
7c7411bc | 1783 | (ptid_t (current_event.dwProcessId, current_event.dwThreadId, 0), |
c559d709 JB |
1784 | current_event.u.CreateThread.hThread, |
1785 | current_event.u.CreateThread.lpThreadLocalBase, | |
1786 | false /* main_thread_p */); | |
711e434b | 1787 | |
1e37c281 JM |
1788 | break; |
1789 | ||
1790 | case EXIT_THREAD_DEBUG_EVENT: | |
0c3d84be | 1791 | DEBUG_EVENTS (("gdb: kernel event for pid=%u tid=0x%x code=%s)\n", |
8a892701 CF |
1792 | (unsigned) current_event.dwProcessId, |
1793 | (unsigned) current_event.dwThreadId, | |
1794 | "EXIT_THREAD_DEBUG_EVENT")); | |
7c7411bc TT |
1795 | windows_delete_thread (ptid_t (current_event.dwProcessId, |
1796 | current_event.dwThreadId, 0), | |
c559d709 JB |
1797 | current_event.u.ExitThread.dwExitCode, |
1798 | false /* main_thread_p */); | |
2eab46b1 | 1799 | th = &dummy_thread_info; |
1e37c281 JM |
1800 | break; |
1801 | ||
1802 | case CREATE_PROCESS_DEBUG_EVENT: | |
0c3d84be | 1803 | DEBUG_EVENTS (("gdb: kernel event for pid=%u tid=0x%x code=%s)\n", |
8a892701 CF |
1804 | (unsigned) current_event.dwProcessId, |
1805 | (unsigned) current_event.dwThreadId, | |
1806 | "CREATE_PROCESS_DEBUG_EVENT")); | |
700b351b | 1807 | CloseHandle (current_event.u.CreateProcessInfo.hFile); |
dfe7f3ac | 1808 | if (++saw_create != 1) |
bf25528d | 1809 | break; |
1e37c281 | 1810 | |
dfe7f3ac | 1811 | current_process_handle = current_event.u.CreateProcessInfo.hProcess; |
581e13c1 | 1812 | /* Add the main thread. */ |
c559d709 | 1813 | th = windows_add_thread |
7c7411bc TT |
1814 | (ptid_t (current_event.dwProcessId, |
1815 | current_event.dwThreadId, 0), | |
c559d709 JB |
1816 | current_event.u.CreateProcessInfo.hThread, |
1817 | current_event.u.CreateProcessInfo.lpThreadLocalBase, | |
1818 | true /* main_thread_p */); | |
e6ad66bd | 1819 | thread_id = current_event.dwThreadId; |
1e37c281 JM |
1820 | break; |
1821 | ||
1822 | case EXIT_PROCESS_DEBUG_EVENT: | |
0c3d84be | 1823 | DEBUG_EVENTS (("gdb: kernel event for pid=%u tid=0x%x code=%s)\n", |
8a892701 CF |
1824 | (unsigned) current_event.dwProcessId, |
1825 | (unsigned) current_event.dwThreadId, | |
1826 | "EXIT_PROCESS_DEBUG_EVENT")); | |
16d905e2 CF |
1827 | if (!windows_initialization_done) |
1828 | { | |
223ffa71 | 1829 | target_terminal::ours (); |
bc1e6c81 | 1830 | target_mourn_inferior (inferior_ptid); |
16d905e2 CF |
1831 | error (_("During startup program exited with code 0x%x."), |
1832 | (unsigned int) current_event.u.ExitProcess.dwExitCode); | |
1833 | } | |
1834 | else if (saw_create == 1) | |
1835 | { | |
7c7411bc TT |
1836 | windows_delete_thread (ptid_t (current_event.dwProcessId, |
1837 | current_event.dwThreadId, 0), | |
c559d709 | 1838 | 0, true /* main_thread_p */); |
559e7e50 EZ |
1839 | DWORD exit_status = current_event.u.ExitProcess.dwExitCode; |
1840 | /* If the exit status looks like a fatal exception, but we | |
1841 | don't recognize the exception's code, make the original | |
1842 | exit status value available, to avoid losing | |
1843 | information. */ | |
1844 | int exit_signal | |
1845 | = WIFSIGNALED (exit_status) ? WTERMSIG (exit_status) : -1; | |
1846 | if (exit_signal == -1) | |
1847 | { | |
1848 | ourstatus->kind = TARGET_WAITKIND_EXITED; | |
1849 | ourstatus->value.integer = exit_status; | |
1850 | } | |
1851 | else | |
1852 | { | |
1853 | ourstatus->kind = TARGET_WAITKIND_SIGNALLED; | |
1854 | ourstatus->value.sig = gdb_signal_from_host (exit_signal); | |
1855 | } | |
8ed5b76e | 1856 | thread_id = current_event.dwThreadId; |
16d905e2 | 1857 | } |
8a892701 | 1858 | break; |
1e37c281 JM |
1859 | |
1860 | case LOAD_DLL_DEBUG_EVENT: | |
0c3d84be | 1861 | DEBUG_EVENTS (("gdb: kernel event for pid=%u tid=0x%x code=%s)\n", |
8a892701 CF |
1862 | (unsigned) current_event.dwProcessId, |
1863 | (unsigned) current_event.dwThreadId, | |
1864 | "LOAD_DLL_DEBUG_EVENT")); | |
700b351b | 1865 | CloseHandle (current_event.u.LoadDll.hFile); |
ea39ad35 | 1866 | if (saw_create != 1 || ! windows_initialization_done) |
dfe7f3ac | 1867 | break; |
bf469271 | 1868 | catch_errors (handle_load_dll); |
450005e7 CF |
1869 | ourstatus->kind = TARGET_WAITKIND_LOADED; |
1870 | ourstatus->value.integer = 0; | |
ab4ee614 | 1871 | thread_id = current_event.dwThreadId; |
1e37c281 JM |
1872 | break; |
1873 | ||
1874 | case UNLOAD_DLL_DEBUG_EVENT: | |
0c3d84be | 1875 | DEBUG_EVENTS (("gdb: kernel event for pid=%u tid=0x%x code=%s)\n", |
8a892701 CF |
1876 | (unsigned) current_event.dwProcessId, |
1877 | (unsigned) current_event.dwThreadId, | |
1878 | "UNLOAD_DLL_DEBUG_EVENT")); | |
ea39ad35 | 1879 | if (saw_create != 1 || ! windows_initialization_done) |
dfe7f3ac | 1880 | break; |
bf469271 | 1881 | catch_errors (handle_unload_dll); |
de1b3c3d PA |
1882 | ourstatus->kind = TARGET_WAITKIND_LOADED; |
1883 | ourstatus->value.integer = 0; | |
ab4ee614 | 1884 | thread_id = current_event.dwThreadId; |
d3ff4a77 | 1885 | break; |
1e37c281 JM |
1886 | |
1887 | case EXCEPTION_DEBUG_EVENT: | |
0c3d84be | 1888 | DEBUG_EVENTS (("gdb: kernel event for pid=%u tid=0x%x code=%s)\n", |
8a892701 CF |
1889 | (unsigned) current_event.dwProcessId, |
1890 | (unsigned) current_event.dwThreadId, | |
1891 | "EXCEPTION_DEBUG_EVENT")); | |
dfe7f3ac CF |
1892 | if (saw_create != 1) |
1893 | break; | |
24cdb46e РИ |
1894 | switch (handle_exception (ourstatus)) |
1895 | { | |
1896 | case HANDLE_EXCEPTION_UNHANDLED: | |
1897 | default: | |
1898 | continue_status = DBG_EXCEPTION_NOT_HANDLED; | |
1899 | break; | |
1900 | case HANDLE_EXCEPTION_HANDLED: | |
1901 | thread_id = current_event.dwThreadId; | |
1902 | break; | |
1903 | case HANDLE_EXCEPTION_IGNORED: | |
1904 | continue_status = DBG_CONTINUE; | |
1905 | break; | |
1906 | } | |
1e37c281 JM |
1907 | break; |
1908 | ||
581e13c1 | 1909 | case OUTPUT_DEBUG_STRING_EVENT: /* Message from the kernel. */ |
0c3d84be | 1910 | DEBUG_EVENTS (("gdb: kernel event for pid=%u tid=0x%x code=%s)\n", |
8a892701 CF |
1911 | (unsigned) current_event.dwProcessId, |
1912 | (unsigned) current_event.dwThreadId, | |
1913 | "OUTPUT_DEBUG_STRING_EVENT")); | |
dfe7f3ac CF |
1914 | if (saw_create != 1) |
1915 | break; | |
e6ad66bd | 1916 | thread_id = handle_output_debug_string (ourstatus); |
1e37c281 | 1917 | break; |
9d3789f7 | 1918 | |
1e37c281 | 1919 | default: |
dfe7f3ac CF |
1920 | if (saw_create != 1) |
1921 | break; | |
0c3d84be | 1922 | printf_unfiltered ("gdb: kernel event for pid=%u tid=0x%x\n", |
d50a0ce2 CV |
1923 | (unsigned) current_event.dwProcessId, |
1924 | (unsigned) current_event.dwThreadId); | |
1925 | printf_unfiltered (" unknown event code %u\n", | |
1926 | (unsigned) current_event.dwDebugEventCode); | |
1e37c281 JM |
1927 | break; |
1928 | } | |
1929 | ||
e6ad66bd | 1930 | if (!thread_id || saw_create != 1) |
a244bdca | 1931 | { |
0a4afda3 TT |
1932 | CHECK (windows_continue (continue_status, desired_stop_thread_id, 0)); |
1933 | } | |
1934 | else if (desired_stop_thread_id != -1 && desired_stop_thread_id != thread_id) | |
1935 | { | |
1936 | /* Pending stop. See the comment by the definition of | |
1937 | "pending_stops" for details on why this is needed. */ | |
1938 | DEBUG_EVENTS (("get_windows_debug_event - " | |
1939 | "unexpected stop in 0x%x (expecting 0x%x)\n", | |
1940 | thread_id, desired_stop_thread_id)); | |
1941 | ||
1942 | if (current_event.dwDebugEventCode == EXCEPTION_DEBUG_EVENT | |
1943 | && (current_event.u.Exception.ExceptionRecord.ExceptionCode | |
1944 | == EXCEPTION_BREAKPOINT) | |
1945 | && windows_initialization_done) | |
1946 | { | |
28688adf TT |
1947 | ptid_t ptid = ptid_t (current_event.dwProcessId, thread_id, 0); |
1948 | th = thread_rec (ptid, INVALIDATE_CONTEXT); | |
0a4afda3 TT |
1949 | th->stopped_at_software_breakpoint = true; |
1950 | } | |
1951 | pending_stops.push_back ({thread_id, *ourstatus, current_event}); | |
1952 | thread_id = 0; | |
1953 | CHECK (windows_continue (continue_status, desired_stop_thread_id, 0)); | |
a244bdca | 1954 | } |
450005e7 | 1955 | else |
9d3789f7 | 1956 | { |
7c7411bc | 1957 | inferior_ptid = ptid_t (current_event.dwProcessId, thread_id, 0); |
3c76026d TT |
1958 | current_windows_thread = th; |
1959 | if (!current_windows_thread) | |
1960 | current_windows_thread = thread_rec (inferior_ptid, | |
1961 | INVALIDATE_CONTEXT); | |
9d3789f7 | 1962 | } |
1e37c281 JM |
1963 | |
1964 | out: | |
e6ad66bd | 1965 | return thread_id; |
1e37c281 JM |
1966 | } |
1967 | ||
2dc38344 | 1968 | /* Wait for interesting events to occur in the target process. */ |
f6ac5f3d PA |
1969 | ptid_t |
1970 | windows_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus, | |
1971 | int options) | |
24e60978 | 1972 | { |
2dc38344 | 1973 | int pid = -1; |
39f77062 | 1974 | |
24e60978 SC |
1975 | /* We loop when we get a non-standard exception rather than return |
1976 | with a SPURIOUS because resume can try and step or modify things, | |
3cee93ac | 1977 | which needs a current_thread->h. But some of these exceptions mark |
24e60978 | 1978 | the birth or death of threads, which mean that the current thread |
581e13c1 | 1979 | isn't necessarily what you think it is. */ |
24e60978 SC |
1980 | |
1981 | while (1) | |
450005e7 | 1982 | { |
c57918b2 | 1983 | int retval; |
2b008701 | 1984 | |
695de547 CF |
1985 | /* If the user presses Ctrl-c while the debugger is waiting |
1986 | for an event, he expects the debugger to interrupt his program | |
1987 | and to get the prompt back. There are two possible situations: | |
1988 | ||
1989 | - The debugger and the program do not share the console, in | |
1990 | which case the Ctrl-c event only reached the debugger. | |
1991 | In that case, the ctrl_c handler will take care of interrupting | |
581e13c1 MS |
1992 | the inferior. Note that this case is working starting with |
1993 | Windows XP. For Windows 2000, Ctrl-C should be pressed in the | |
695de547 CF |
1994 | inferior console. |
1995 | ||
1996 | - The debugger and the program share the same console, in which | |
1997 | case both debugger and inferior will receive the Ctrl-c event. | |
1998 | In that case the ctrl_c handler will ignore the event, as the | |
1999 | Ctrl-c event generated inside the inferior will trigger the | |
2000 | expected debug event. | |
2001 | ||
2002 | FIXME: brobecker/2008-05-20: If the inferior receives the | |
2003 | signal first and the delay until GDB receives that signal | |
2004 | is sufficiently long, GDB can sometimes receive the SIGINT | |
2005 | after we have unblocked the CTRL+C handler. This would | |
2006 | lead to the debugger stopping prematurely while handling | |
2007 | the new-thread event that comes with the handling of the SIGINT | |
2008 | inside the inferior, and then stop again immediately when | |
2009 | the user tries to resume the execution in the inferior. | |
2010 | This is a classic race that we should try to fix one day. */ | |
2011 | SetConsoleCtrlHandler (&ctrl_c_handler, TRUE); | |
5b6d1e4f | 2012 | retval = get_windows_debug_event (pid, ourstatus); |
695de547 | 2013 | SetConsoleCtrlHandler (&ctrl_c_handler, FALSE); |
c57918b2 | 2014 | |
450005e7 | 2015 | if (retval) |
0a4afda3 TT |
2016 | { |
2017 | ptid_t result = ptid_t (current_event.dwProcessId, retval, 0); | |
2018 | ||
3c76026d | 2019 | if (current_windows_thread != nullptr) |
0a4afda3 | 2020 | { |
3c76026d | 2021 | current_windows_thread->stopped_at_software_breakpoint = false; |
0a4afda3 TT |
2022 | if (current_event.dwDebugEventCode == EXCEPTION_DEBUG_EVENT |
2023 | && (current_event.u.Exception.ExceptionRecord.ExceptionCode | |
2024 | == EXCEPTION_BREAKPOINT) | |
2025 | && windows_initialization_done) | |
3c76026d | 2026 | current_windows_thread->stopped_at_software_breakpoint = true; |
0a4afda3 TT |
2027 | } |
2028 | ||
2029 | return result; | |
2030 | } | |
450005e7 CF |
2031 | else |
2032 | { | |
2033 | int detach = 0; | |
3cee93ac | 2034 | |
98bbd631 AC |
2035 | if (deprecated_ui_loop_hook != NULL) |
2036 | detach = deprecated_ui_loop_hook (0); | |
0714f9bf | 2037 | |
450005e7 | 2038 | if (detach) |
f6ac5f3d | 2039 | kill (); |
450005e7 CF |
2040 | } |
2041 | } | |
24e60978 SC |
2042 | } |
2043 | ||
ea39ad35 JB |
2044 | /* Iterate over all DLLs currently mapped by our inferior, and |
2045 | add them to our list of solibs. */ | |
94481b8c JB |
2046 | |
2047 | static void | |
ea39ad35 | 2048 | windows_add_all_dlls (void) |
94481b8c | 2049 | { |
94481b8c JB |
2050 | HMODULE dummy_hmodule; |
2051 | DWORD cb_needed; | |
2052 | HMODULE *hmodules; | |
2053 | int i; | |
2054 | ||
46f9f931 HD |
2055 | #ifdef __x86_64__ |
2056 | if (wow64_process) | |
2057 | { | |
2058 | if (EnumProcessModulesEx (current_process_handle, &dummy_hmodule, | |
2059 | sizeof (HMODULE), &cb_needed, | |
2060 | LIST_MODULES_32BIT) == 0) | |
2061 | return; | |
2062 | } | |
2063 | else | |
2064 | #endif | |
2065 | { | |
2066 | if (EnumProcessModules (current_process_handle, &dummy_hmodule, | |
2067 | sizeof (HMODULE), &cb_needed) == 0) | |
2068 | return; | |
2069 | } | |
94481b8c JB |
2070 | |
2071 | if (cb_needed < 1) | |
2072 | return; | |
2073 | ||
2074 | hmodules = (HMODULE *) alloca (cb_needed); | |
46f9f931 HD |
2075 | #ifdef __x86_64__ |
2076 | if (wow64_process) | |
2077 | { | |
2078 | if (EnumProcessModulesEx (current_process_handle, hmodules, | |
2079 | cb_needed, &cb_needed, | |
2080 | LIST_MODULES_32BIT) == 0) | |
2081 | return; | |
2082 | } | |
2083 | else | |
2084 | #endif | |
2085 | { | |
2086 | if (EnumProcessModules (current_process_handle, hmodules, | |
2087 | cb_needed, &cb_needed) == 0) | |
2088 | return; | |
2089 | } | |
94481b8c | 2090 | |
d503b685 HD |
2091 | char system_dir[__PMAX]; |
2092 | char syswow_dir[__PMAX]; | |
2093 | size_t system_dir_len = 0; | |
ebea7626 HD |
2094 | bool convert_syswow_dir = false; |
2095 | #ifdef __x86_64__ | |
d503b685 | 2096 | if (wow64_process) |
ebea7626 | 2097 | #endif |
d503b685 | 2098 | { |
ebea7626 HD |
2099 | /* This fails on 32bit Windows because it has no SysWOW64 directory, |
2100 | and in this case a path conversion isn't necessary. */ | |
2101 | UINT len = GetSystemWow64DirectoryA (syswow_dir, sizeof (syswow_dir)); | |
2102 | if (len > 0) | |
2103 | { | |
2104 | /* Check that we have passed a large enough buffer. */ | |
2105 | gdb_assert (len < sizeof (syswow_dir)); | |
2106 | ||
2107 | len = GetSystemDirectoryA (system_dir, sizeof (system_dir)); | |
2108 | /* Error check. */ | |
2109 | gdb_assert (len != 0); | |
2110 | /* Check that we have passed a large enough buffer. */ | |
2111 | gdb_assert (len < sizeof (system_dir)); | |
2112 | ||
2113 | strcat (system_dir, "\\"); | |
2114 | strcat (syswow_dir, "\\"); | |
2115 | system_dir_len = strlen (system_dir); | |
2116 | ||
2117 | convert_syswow_dir = true; | |
2118 | } | |
2119 | ||
d503b685 | 2120 | } |
ea39ad35 | 2121 | for (i = 1; i < (int) (cb_needed / sizeof (HMODULE)); i++) |
94481b8c JB |
2122 | { |
2123 | MODULEINFO mi; | |
774f74c2 PM |
2124 | #ifdef __USEWIDE |
2125 | wchar_t dll_name[__PMAX]; | |
d503b685 | 2126 | char dll_name_mb[__PMAX]; |
774f74c2 | 2127 | #else |
94481b8c | 2128 | char dll_name[__PMAX]; |
774f74c2 | 2129 | #endif |
d503b685 | 2130 | const char *name; |
94481b8c JB |
2131 | if (GetModuleInformation (current_process_handle, hmodules[i], |
2132 | &mi, sizeof (mi)) == 0) | |
2133 | continue; | |
2134 | if (GetModuleFileNameEx (current_process_handle, hmodules[i], | |
2135 | dll_name, sizeof (dll_name)) == 0) | |
2136 | continue; | |
774f74c2 | 2137 | #ifdef __USEWIDE |
d503b685 HD |
2138 | wcstombs (dll_name_mb, dll_name, __PMAX); |
2139 | name = dll_name_mb; | |
774f74c2 PM |
2140 | #else |
2141 | name = dll_name; | |
2142 | #endif | |
ebea7626 | 2143 | /* Convert the DLL path of 32bit processes returned by |
d503b685 HD |
2144 | GetModuleFileNameEx from the 64bit system directory to the |
2145 | 32bit syswow64 directory if necessary. */ | |
2146 | std::string syswow_dll_path; | |
ebea7626 | 2147 | if (convert_syswow_dir |
d503b685 HD |
2148 | && strncasecmp (name, system_dir, system_dir_len) == 0 |
2149 | && strchr (name + system_dir_len, '\\') == nullptr) | |
2150 | { | |
2151 | syswow_dll_path = syswow_dir; | |
2152 | syswow_dll_path += name + system_dir_len; | |
2153 | name = syswow_dll_path.c_str(); | |
2154 | } | |
ea39ad35 JB |
2155 | |
2156 | solib_end->next = windows_make_so (name, mi.lpBaseOfDll); | |
2157 | solib_end = solib_end->next; | |
94481b8c JB |
2158 | } |
2159 | } | |
2160 | ||
9d3789f7 | 2161 | static void |
dc05df57 | 2162 | do_initial_windows_stuff (struct target_ops *ops, DWORD pid, int attaching) |
9d3789f7 | 2163 | { |
fa4ba8da | 2164 | int i; |
d6b48e9c | 2165 | struct inferior *inf; |
9d3789f7 | 2166 | |
a493e3e2 | 2167 | last_sig = GDB_SIGNAL_0; |
9d3789f7 CF |
2168 | event_count = 0; |
2169 | exception_count = 0; | |
bf25528d | 2170 | open_process_used = 0; |
fa4ba8da | 2171 | debug_registers_changed = 0; |
dfe7f3ac | 2172 | debug_registers_used = 0; |
fa4ba8da PM |
2173 | for (i = 0; i < sizeof (dr) / sizeof (dr[0]); i++) |
2174 | dr[i] = 0; | |
10325bc5 | 2175 | #ifdef __CYGWIN__ |
de1b3c3d | 2176 | cygwin_load_start = cygwin_load_end = 0; |
10325bc5 | 2177 | #endif |
9d3789f7 CF |
2178 | current_event.dwProcessId = pid; |
2179 | memset (¤t_event, 0, sizeof (current_event)); | |
6a3cb8e8 PA |
2180 | if (!target_is_pushed (ops)) |
2181 | push_target (ops); | |
cb851954 | 2182 | disable_breakpoints_in_shlibs (); |
dc05df57 | 2183 | windows_clear_solib (); |
88056fbb | 2184 | clear_proceed_status (0); |
9d3789f7 CF |
2185 | init_wait_for_inferior (); |
2186 | ||
46f9f931 HD |
2187 | #ifdef __x86_64__ |
2188 | ignore_first_breakpoint = !attaching && wow64_process; | |
2189 | ||
2190 | if (!wow64_process) | |
2191 | { | |
2192 | windows_set_context_register_offsets (amd64_mappings); | |
2193 | windows_set_segment_register_p (amd64_windows_segment_register_p); | |
2194 | } | |
2195 | else | |
2196 | #endif | |
2197 | { | |
2198 | windows_set_context_register_offsets (i386_mappings); | |
2199 | windows_set_segment_register_p (i386_windows_segment_register_p); | |
2200 | } | |
2201 | ||
6c95b8df PA |
2202 | inf = current_inferior (); |
2203 | inferior_appeared (inf, pid); | |
181e7f93 | 2204 | inf->attach_flag = attaching; |
7f9f62ba | 2205 | |
9f9d052e PM |
2206 | /* Make the new process the current inferior, so terminal handling |
2207 | can rely on it. When attaching, we don't know about any thread | |
2208 | id here, but that's OK --- nothing should be referencing the | |
dc05df57 | 2209 | current thread until we report an event out of windows_wait. */ |
f2907e49 | 2210 | inferior_ptid = ptid_t (pid); |
9f9d052e | 2211 | |
223ffa71 TT |
2212 | target_terminal::init (); |
2213 | target_terminal::inferior (); | |
9d3789f7 | 2214 | |
16d905e2 | 2215 | windows_initialization_done = 0; |
c72f45d1 | 2216 | |
9d3789f7 CF |
2217 | while (1) |
2218 | { | |
c72f45d1 PA |
2219 | struct target_waitstatus status; |
2220 | ||
f6ac5f3d | 2221 | ops->wait (minus_one_ptid, &status, 0); |
c72f45d1 PA |
2222 | |
2223 | /* Note windows_wait returns TARGET_WAITKIND_SPURIOUS for thread | |
2224 | events. */ | |
2225 | if (status.kind != TARGET_WAITKIND_LOADED | |
2226 | && status.kind != TARGET_WAITKIND_SPURIOUS) | |
9d3789f7 | 2227 | break; |
c72f45d1 | 2228 | |
f6ac5f3d | 2229 | ops->resume (minus_one_ptid, 0, GDB_SIGNAL_0); |
9d3789f7 | 2230 | } |
eff8332b | 2231 | |
ea39ad35 | 2232 | /* Now that the inferior has been started and all DLLs have been mapped, |
3be75f87 JB |
2233 | we can iterate over all DLLs and load them in. |
2234 | ||
2235 | We avoid doing it any earlier because, on certain versions of Windows, | |
2236 | LOAD_DLL_DEBUG_EVENTs are sometimes not complete. In particular, | |
2237 | we have seen on Windows 8.1 that the ntdll.dll load event does not | |
2238 | include the DLL name, preventing us from creating an associated SO. | |
2239 | A possible explanation is that ntdll.dll might be mapped before | |
2240 | the SO info gets created by the Windows system -- ntdll.dll is | |
2241 | the first DLL to be reported via LOAD_DLL_DEBUG_EVENT and other DLLs | |
2242 | do not seem to suffer from that problem. | |
2243 | ||
2244 | Rather than try to work around this sort of issue, it is much | |
2245 | simpler to just ignore DLL load/unload events during the startup | |
2246 | phase, and then process them all in one batch now. */ | |
ea39ad35 | 2247 | windows_add_all_dlls (); |
94481b8c | 2248 | |
16d905e2 | 2249 | windows_initialization_done = 1; |
9d3789f7 CF |
2250 | return; |
2251 | } | |
2252 | ||
616a9dc4 CV |
2253 | /* Try to set or remove a user privilege to the current process. Return -1 |
2254 | if that fails, the previous setting of that privilege otherwise. | |
2255 | ||
2256 | This code is copied from the Cygwin source code and rearranged to allow | |
2257 | dynamically loading of the needed symbols from advapi32 which is only | |
581e13c1 | 2258 | available on NT/2K/XP. */ |
616a9dc4 CV |
2259 | static int |
2260 | set_process_privilege (const char *privilege, BOOL enable) | |
2261 | { | |
616a9dc4 CV |
2262 | HANDLE token_hdl = NULL; |
2263 | LUID restore_priv; | |
2264 | TOKEN_PRIVILEGES new_priv, orig_priv; | |
2265 | int ret = -1; | |
2266 | DWORD size; | |
2267 | ||
616a9dc4 CV |
2268 | if (!OpenProcessToken (GetCurrentProcess (), |
2269 | TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES, | |
2270 | &token_hdl)) | |
2271 | goto out; | |
2272 | ||
418c6cb3 | 2273 | if (!LookupPrivilegeValueA (NULL, privilege, &restore_priv)) |
616a9dc4 CV |
2274 | goto out; |
2275 | ||
2276 | new_priv.PrivilegeCount = 1; | |
2277 | new_priv.Privileges[0].Luid = restore_priv; | |
2278 | new_priv.Privileges[0].Attributes = enable ? SE_PRIVILEGE_ENABLED : 0; | |
2279 | ||
2280 | if (!AdjustTokenPrivileges (token_hdl, FALSE, &new_priv, | |
295732ea | 2281 | sizeof orig_priv, &orig_priv, &size)) |
616a9dc4 CV |
2282 | goto out; |
2283 | #if 0 | |
2284 | /* Disabled, otherwise every `attach' in an unprivileged user session | |
2285 | would raise the "Failed to get SE_DEBUG_NAME privilege" warning in | |
581e13c1 | 2286 | windows_attach(). */ |
616a9dc4 | 2287 | /* AdjustTokenPrivileges returns TRUE even if the privilege could not |
581e13c1 | 2288 | be enabled. GetLastError () returns an correct error code, though. */ |
616a9dc4 CV |
2289 | if (enable && GetLastError () == ERROR_NOT_ALL_ASSIGNED) |
2290 | goto out; | |
2291 | #endif | |
2292 | ||
2293 | ret = orig_priv.Privileges[0].Attributes == SE_PRIVILEGE_ENABLED ? 1 : 0; | |
2294 | ||
2295 | out: | |
2296 | if (token_hdl) | |
2297 | CloseHandle (token_hdl); | |
2298 | ||
2299 | return ret; | |
2300 | } | |
2301 | ||
02cc9f49 | 2302 | /* Attach to process PID, then initialize for debugging it. */ |
f6ac5f3d PA |
2303 | |
2304 | void | |
2305 | windows_nat_target::attach (const char *args, int from_tty) | |
24e60978 SC |
2306 | { |
2307 | BOOL ok; | |
559e75c0 | 2308 | DWORD pid; |
24e60978 | 2309 | |
74164c56 | 2310 | pid = parse_pid_to_attach (args); |
24e60978 | 2311 | |
616a9dc4 CV |
2312 | if (set_process_privilege (SE_DEBUG_NAME, TRUE) < 0) |
2313 | { | |
2314 | printf_unfiltered ("Warning: Failed to get SE_DEBUG_NAME privilege\n"); | |
581e13c1 MS |
2315 | printf_unfiltered ("This can cause attach to " |
2316 | "fail on Windows NT/2K/XP\n"); | |
616a9dc4 CV |
2317 | } |
2318 | ||
dc05df57 | 2319 | windows_init_thread_list (); |
9d3789f7 | 2320 | ok = DebugActiveProcess (pid); |
91a175b3 | 2321 | saw_create = 0; |
24e60978 | 2322 | |
10325bc5 | 2323 | #ifdef __CYGWIN__ |
24e60978 | 2324 | if (!ok) |
baa93fa6 | 2325 | { |
581e13c1 | 2326 | /* Try fall back to Cygwin pid. */ |
baa93fa6 CF |
2327 | pid = cygwin_internal (CW_CYGWIN_PID_TO_WINPID, pid); |
2328 | ||
2329 | if (pid > 0) | |
2330 | ok = DebugActiveProcess (pid); | |
10325bc5 PA |
2331 | } |
2332 | #endif | |
baa93fa6 | 2333 | |
10325bc5 | 2334 | if (!ok) |
c9739b6a TT |
2335 | error (_("Can't attach to process %u (error %u)"), |
2336 | (unsigned) pid, (unsigned) GetLastError ()); | |
24e60978 | 2337 | |
2b008701 | 2338 | DebugSetProcessKillOnExit (FALSE); |
3ade5333 | 2339 | |
24e60978 SC |
2340 | if (from_tty) |
2341 | { | |
d9fa87f4 | 2342 | const char *exec_file = get_exec_file (0); |
24e60978 SC |
2343 | |
2344 | if (exec_file) | |
2345 | printf_unfiltered ("Attaching to program `%s', %s\n", exec_file, | |
a068643d | 2346 | target_pid_to_str (ptid_t (pid)).c_str ()); |
24e60978 SC |
2347 | else |
2348 | printf_unfiltered ("Attaching to %s\n", | |
a068643d | 2349 | target_pid_to_str (ptid_t (pid)).c_str ()); |
24e60978 SC |
2350 | } |
2351 | ||
46f9f931 HD |
2352 | #ifdef __x86_64__ |
2353 | HANDLE h = OpenProcess (PROCESS_QUERY_INFORMATION, FALSE, pid); | |
2354 | if (h != NULL) | |
2355 | { | |
2356 | BOOL wow64; | |
2357 | if (IsWow64Process (h, &wow64)) | |
2358 | wow64_process = wow64; | |
2359 | CloseHandle (h); | |
2360 | } | |
2361 | #endif | |
2362 | ||
f6ac5f3d | 2363 | do_initial_windows_stuff (this, pid, 1); |
223ffa71 | 2364 | target_terminal::ours (); |
24e60978 SC |
2365 | } |
2366 | ||
f6ac5f3d PA |
2367 | void |
2368 | windows_nat_target::detach (inferior *inf, int from_tty) | |
24e60978 | 2369 | { |
02cc9f49 CV |
2370 | int detached = 1; |
2371 | ||
8473b447 | 2372 | ptid_t ptid = minus_one_ptid; |
f6ac5f3d | 2373 | resume (ptid, 0, GDB_SIGNAL_0); |
96998ce7 | 2374 | |
2b008701 CF |
2375 | if (!DebugActiveProcessStop (current_event.dwProcessId)) |
2376 | { | |
d50a0ce2 CV |
2377 | error (_("Can't detach process %u (error %u)"), |
2378 | (unsigned) current_event.dwProcessId, (unsigned) GetLastError ()); | |
2b008701 | 2379 | detached = 0; |
02cc9f49 | 2380 | } |
2b008701 CF |
2381 | DebugSetProcessKillOnExit (FALSE); |
2382 | ||
02cc9f49 | 2383 | if (detached && from_tty) |
24e60978 | 2384 | { |
a121b7c1 | 2385 | const char *exec_file = get_exec_file (0); |
24e60978 SC |
2386 | if (exec_file == 0) |
2387 | exec_file = ""; | |
d50a0ce2 CV |
2388 | printf_unfiltered ("Detaching from program: %s, Pid %u\n", exec_file, |
2389 | (unsigned) current_event.dwProcessId); | |
24e60978 | 2390 | } |
7f9f62ba | 2391 | |
df7e5265 | 2392 | x86_cleanup_dregs (); |
39f77062 | 2393 | inferior_ptid = null_ptid; |
b7a08269 | 2394 | detach_inferior (inf); |
7f9f62ba | 2395 | |
f6ac5f3d | 2396 | maybe_unpush_target (); |
24e60978 SC |
2397 | } |
2398 | ||
47f7ffdb JB |
2399 | /* Try to determine the executable filename. |
2400 | ||
2401 | EXE_NAME_RET is a pointer to a buffer whose size is EXE_NAME_MAX_LEN. | |
2402 | ||
2403 | Upon success, the filename is stored inside EXE_NAME_RET, and | |
2404 | this function returns nonzero. | |
2405 | ||
2406 | Otherwise, this function returns zero and the contents of | |
2407 | EXE_NAME_RET is undefined. */ | |
2408 | ||
2409 | static int | |
2410 | windows_get_exec_module_filename (char *exe_name_ret, size_t exe_name_max_len) | |
2411 | { | |
2412 | DWORD len; | |
2413 | HMODULE dh_buf; | |
2414 | DWORD cbNeeded; | |
2415 | ||
2416 | cbNeeded = 0; | |
46f9f931 HD |
2417 | #ifdef __x86_64__ |
2418 | if (wow64_process) | |
2419 | { | |
2420 | if (!EnumProcessModulesEx (current_process_handle, &dh_buf, | |
2421 | sizeof (HMODULE), &cbNeeded, | |
2422 | LIST_MODULES_32BIT) || !cbNeeded) | |
2423 | return 0; | |
2424 | } | |
2425 | else | |
2426 | #endif | |
2427 | { | |
2428 | if (!EnumProcessModules (current_process_handle, &dh_buf, | |
2429 | sizeof (HMODULE), &cbNeeded) || !cbNeeded) | |
2430 | return 0; | |
2431 | } | |
47f7ffdb JB |
2432 | |
2433 | /* We know the executable is always first in the list of modules, | |
2434 | which we just fetched. So no need to fetch more. */ | |
2435 | ||
2436 | #ifdef __CYGWIN__ | |
2437 | { | |
2438 | /* Cygwin prefers that the path be in /x/y/z format, so extract | |
2439 | the filename into a temporary buffer first, and then convert it | |
2440 | to POSIX format into the destination buffer. */ | |
0ae534d2 | 2441 | cygwin_buf_t *pathbuf = (cygwin_buf_t *) alloca (exe_name_max_len * sizeof (cygwin_buf_t)); |
47f7ffdb JB |
2442 | |
2443 | len = GetModuleFileNameEx (current_process_handle, | |
2444 | dh_buf, pathbuf, exe_name_max_len); | |
2445 | if (len == 0) | |
2446 | error (_("Error getting executable filename: %u."), | |
2447 | (unsigned) GetLastError ()); | |
2448 | if (cygwin_conv_path (CCP_WIN_W_TO_POSIX, pathbuf, exe_name_ret, | |
2449 | exe_name_max_len) < 0) | |
2450 | error (_("Error converting executable filename to POSIX: %d."), errno); | |
2451 | } | |
2452 | #else | |
2453 | len = GetModuleFileNameEx (current_process_handle, | |
2454 | dh_buf, exe_name_ret, exe_name_max_len); | |
2455 | if (len == 0) | |
2456 | error (_("Error getting executable filename: %u."), | |
2457 | (unsigned) GetLastError ()); | |
2458 | #endif | |
2459 | ||
2460 | return 1; /* success */ | |
2461 | } | |
2462 | ||
2463 | /* The pid_to_exec_file target_ops method for this platform. */ | |
2464 | ||
f6ac5f3d PA |
2465 | char * |
2466 | windows_nat_target::pid_to_exec_file (int pid) | |
47216e51 | 2467 | { |
b3c613f2 | 2468 | static char path[__PMAX]; |
10325bc5 | 2469 | #ifdef __CYGWIN__ |
581e13c1 | 2470 | /* Try to find exe name as symlink target of /proc/<pid>/exe. */ |
33605d39 CF |
2471 | int nchars; |
2472 | char procexe[sizeof ("/proc/4294967295/exe")]; | |
08850b56 PM |
2473 | |
2474 | xsnprintf (procexe, sizeof (procexe), "/proc/%u/exe", pid); | |
33605d39 CF |
2475 | nchars = readlink (procexe, path, sizeof(path)); |
2476 | if (nchars > 0 && nchars < sizeof (path)) | |
47216e51 | 2477 | { |
33605d39 CF |
2478 | path[nchars] = '\0'; /* Got it */ |
2479 | return path; | |
47216e51 | 2480 | } |
10325bc5 PA |
2481 | #endif |
2482 | ||
33605d39 | 2483 | /* If we get here then either Cygwin is hosed, this isn't a Cygwin version |
581e13c1 | 2484 | of gdb, or we're trying to debug a non-Cygwin windows executable. */ |
47f7ffdb | 2485 | if (!windows_get_exec_module_filename (path, sizeof (path))) |
33605d39 CF |
2486 | path[0] = '\0'; |
2487 | ||
2488 | return path; | |
47216e51 CV |
2489 | } |
2490 | ||
24e60978 SC |
2491 | /* Print status information about what we're accessing. */ |
2492 | ||
f6ac5f3d PA |
2493 | void |
2494 | windows_nat_target::files_info () | |
24e60978 | 2495 | { |
181e7f93 PA |
2496 | struct inferior *inf = current_inferior (); |
2497 | ||
24e60978 | 2498 | printf_unfiltered ("\tUsing the running image of %s %s.\n", |
181e7f93 | 2499 | inf->attach_flag ? "attached" : "child", |
a068643d | 2500 | target_pid_to_str (inferior_ptid).c_str ()); |
24e60978 SC |
2501 | } |
2502 | ||
cd44747c PM |
2503 | /* Modify CreateProcess parameters for use of a new separate console. |
2504 | Parameters are: | |
2505 | *FLAGS: DWORD parameter for general process creation flags. | |
2506 | *SI: STARTUPINFO structure, for which the console window size and | |
2507 | console buffer size is filled in if GDB is running in a console. | |
2508 | to create the new console. | |
2509 | The size of the used font is not available on all versions of | |
2510 | Windows OS. Furthermore, the current font might not be the default | |
2511 | font, but this is still better than before. | |
2512 | If the windows and buffer sizes are computed, | |
2513 | SI->DWFLAGS is changed so that this information is used | |
2514 | by CreateProcess function. */ | |
2515 | ||
2516 | static void | |
2517 | windows_set_console_info (STARTUPINFO *si, DWORD *flags) | |
2518 | { | |
2519 | HANDLE hconsole = CreateFile ("CONOUT$", GENERIC_READ | GENERIC_WRITE, | |
2520 | FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0); | |
2521 | ||
2522 | if (hconsole != INVALID_HANDLE_VALUE) | |
2523 | { | |
2524 | CONSOLE_SCREEN_BUFFER_INFO sbinfo; | |
2525 | COORD font_size; | |
2526 | CONSOLE_FONT_INFO cfi; | |
2527 | ||
2528 | GetCurrentConsoleFont (hconsole, FALSE, &cfi); | |
2529 | font_size = GetConsoleFontSize (hconsole, cfi.nFont); | |
2530 | GetConsoleScreenBufferInfo(hconsole, &sbinfo); | |
2531 | si->dwXSize = sbinfo.srWindow.Right - sbinfo.srWindow.Left + 1; | |
2532 | si->dwYSize = sbinfo.srWindow.Bottom - sbinfo.srWindow.Top + 1; | |
2533 | if (font_size.X) | |
2534 | si->dwXSize *= font_size.X; | |
2535 | else | |
2536 | si->dwXSize *= 8; | |
2537 | if (font_size.Y) | |
2538 | si->dwYSize *= font_size.Y; | |
2539 | else | |
2540 | si->dwYSize *= 12; | |
2541 | si->dwXCountChars = sbinfo.dwSize.X; | |
2542 | si->dwYCountChars = sbinfo.dwSize.Y; | |
2543 | si->dwFlags |= STARTF_USESIZE | STARTF_USECOUNTCHARS; | |
2544 | } | |
2545 | *flags |= CREATE_NEW_CONSOLE; | |
2546 | } | |
2547 | ||
c93dbcba EZ |
2548 | #ifndef __CYGWIN__ |
2549 | /* Function called by qsort to sort environment strings. */ | |
2550 | ||
2551 | static int | |
2552 | envvar_cmp (const void *a, const void *b) | |
2553 | { | |
2554 | const char **p = (const char **) a; | |
2555 | const char **q = (const char **) b; | |
2556 | return strcasecmp (*p, *q); | |
2557 | } | |
2558 | #endif | |
2559 | ||
b7ff339d CV |
2560 | #ifdef __CYGWIN__ |
2561 | static void | |
2562 | clear_win32_environment (char **env) | |
2563 | { | |
2564 | int i; | |
2565 | size_t len; | |
2566 | wchar_t *copy = NULL, *equalpos; | |
2567 | ||
2568 | for (i = 0; env[i] && *env[i]; i++) | |
2569 | { | |
2570 | len = mbstowcs (NULL, env[i], 0) + 1; | |
2571 | copy = (wchar_t *) xrealloc (copy, len * sizeof (wchar_t)); | |
2572 | mbstowcs (copy, env[i], len); | |
2573 | equalpos = wcschr (copy, L'='); | |
2574 | if (equalpos) | |
2575 | *equalpos = L'\0'; | |
2576 | SetEnvironmentVariableW (copy, NULL); | |
2577 | } | |
2578 | xfree (copy); | |
2579 | } | |
2580 | #endif | |
2581 | ||
8ba42bc5 EZ |
2582 | #ifndef __CYGWIN__ |
2583 | ||
2584 | /* Redirection of inferior I/O streams for native MS-Windows programs. | |
2585 | Unlike on Unix, where this is handled by invoking the inferior via | |
2586 | the shell, on MS-Windows we need to emulate the cmd.exe shell. | |
2587 | ||
2588 | The official documentation of the cmd.exe redirection features is here: | |
2589 | ||
2590 | http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx | |
2591 | ||
2592 | (That page talks about Windows XP, but there's no newer | |
2593 | documentation, so we assume later versions of cmd.exe didn't change | |
2594 | anything.) | |
2595 | ||
2596 | Caveat: the documentation on that page seems to include a few lies. | |
2597 | For example, it describes strange constructs 1<&2 and 2<&1, which | |
2598 | seem to work only when 1>&2 resp. 2>&1 would make sense, and so I | |
2599 | think the cmd.exe parser of the redirection symbols simply doesn't | |
2600 | care about the < vs > distinction in these cases. Therefore, the | |
2601 | supported features are explicitly documented below. | |
2602 | ||
2603 | The emulation below aims at supporting all the valid use cases | |
2604 | supported by cmd.exe, which include: | |
2605 | ||
2606 | < FILE redirect standard input from FILE | |
2607 | 0< FILE redirect standard input from FILE | |
2608 | <&N redirect standard input from file descriptor N | |
2609 | 0<&N redirect standard input from file descriptor N | |
2610 | > FILE redirect standard output to FILE | |
2611 | >> FILE append standard output to FILE | |
2612 | 1>> FILE append standard output to FILE | |
2613 | >&N redirect standard output to file descriptor N | |
2614 | 1>&N redirect standard output to file descriptor N | |
2615 | >>&N append standard output to file descriptor N | |
2616 | 1>>&N append standard output to file descriptor N | |
2617 | 2> FILE redirect standard error to FILE | |
2618 | 2>> FILE append standard error to FILE | |
2619 | 2>&N redirect standard error to file descriptor N | |
2620 | 2>>&N append standard error to file descriptor N | |
2621 | ||
2622 | Note that using N > 2 in the above construct is supported, but | |
2623 | requires that the corresponding file descriptor be open by some | |
2624 | means elsewhere or outside GDB. Also note that using ">&0" or | |
2625 | "<&2" will generally fail, because the file descriptor redirected | |
2626 | from is normally open in an incompatible mode (e.g., FD 0 is open | |
2627 | for reading only). IOW, use of such tricks is not recommended; | |
2628 | you are on your own. | |
2629 | ||
2630 | We do NOT support redirection of file descriptors above 2, as in | |
2631 | "3>SOME-FILE", because MinGW compiled programs don't (supporting | |
2632 | that needs special handling in the startup code that MinGW | |
2633 | doesn't have). Pipes are also not supported. | |
2634 | ||
2635 | As for invalid use cases, where the redirection contains some | |
2636 | error, the emulation below will detect that and produce some | |
2637 | error and/or failure. But the behavior in those cases is not | |
2638 | bug-for-bug compatible with what cmd.exe does in those cases. | |
2639 | That's because what cmd.exe does then is not well defined, and | |
2640 | seems to be a side effect of the cmd.exe parsing of the command | |
2641 | line more than anything else. For example, try redirecting to an | |
2642 | invalid file name, as in "> foo:bar". | |
2643 | ||
2644 | There are also minor syntactic deviations from what cmd.exe does | |
2645 | in some corner cases. For example, it doesn't support the likes | |
2646 | of "> &foo" to mean redirect to file named literally "&foo"; we | |
2647 | do support that here, because that, too, sounds like some issue | |
2648 | with the cmd.exe parser. Another nicety is that we support | |
2649 | redirection targets that use file names with forward slashes, | |
2650 | something cmd.exe doesn't -- this comes in handy since GDB | |
2651 | file-name completion can be used when typing the command line for | |
2652 | the inferior. */ | |
2653 | ||
2654 | /* Support routines for redirecting standard handles of the inferior. */ | |
2655 | ||
2656 | /* Parse a single redirection spec, open/duplicate the specified | |
2657 | file/fd, and assign the appropriate value to one of the 3 standard | |
2658 | file descriptors. */ | |
2659 | static int | |
2660 | redir_open (const char *redir_string, int *inp, int *out, int *err) | |
2661 | { | |
2662 | int *fd, ref_fd = -2; | |
2663 | int mode; | |
2664 | const char *fname = redir_string + 1; | |
2665 | int rc = *redir_string; | |
2666 | ||
2667 | switch (rc) | |
2668 | { | |
2669 | case '0': | |
2670 | fname++; | |
2671 | /* FALLTHROUGH */ | |
2672 | case '<': | |
2673 | fd = inp; | |
2674 | mode = O_RDONLY; | |
2675 | break; | |
2676 | case '1': case '2': | |
2677 | fname++; | |
2678 | /* FALLTHROUGH */ | |
2679 | case '>': | |
2680 | fd = (rc == '2') ? err : out; | |
2681 | mode = O_WRONLY | O_CREAT; | |
2682 | if (*fname == '>') | |
2683 | { | |
2684 | fname++; | |
2685 | mode |= O_APPEND; | |
2686 | } | |
2687 | else | |
2688 | mode |= O_TRUNC; | |
2689 | break; | |
2690 | default: | |
2691 | return -1; | |
2692 | } | |
2693 | ||
2694 | if (*fname == '&' && '0' <= fname[1] && fname[1] <= '9') | |
2695 | { | |
2696 | /* A reference to a file descriptor. */ | |
2697 | char *fdtail; | |
2698 | ref_fd = (int) strtol (fname + 1, &fdtail, 10); | |
2699 | if (fdtail > fname + 1 && *fdtail == '\0') | |
2700 | { | |
2701 | /* Don't allow redirection when open modes are incompatible. */ | |
2702 | if ((ref_fd == 0 && (fd == out || fd == err)) | |
2703 | || ((ref_fd == 1 || ref_fd == 2) && fd == inp)) | |
2704 | { | |
2705 | errno = EPERM; | |
2706 | return -1; | |
2707 | } | |
2708 | if (ref_fd == 0) | |
2709 | ref_fd = *inp; | |
2710 | else if (ref_fd == 1) | |
2711 | ref_fd = *out; | |
2712 | else if (ref_fd == 2) | |
2713 | ref_fd = *err; | |
2714 | } | |
2715 | else | |
2716 | { | |
2717 | errno = EBADF; | |
2718 | return -1; | |
2719 | } | |
2720 | } | |
2721 | else | |
2722 | fname++; /* skip the separator space */ | |
2723 | /* If the descriptor is already open, close it. This allows | |
2724 | multiple specs of redirections for the same stream, which is | |
2725 | somewhat nonsensical, but still valid and supported by cmd.exe. | |
2726 | (But cmd.exe only opens a single file in this case, the one | |
2727 | specified by the last redirection spec on the command line.) */ | |
2728 | if (*fd >= 0) | |
2729 | _close (*fd); | |
2730 | if (ref_fd == -2) | |
2731 | { | |
2732 | *fd = _open (fname, mode, _S_IREAD | _S_IWRITE); | |
2733 | if (*fd < 0) | |
2734 | return -1; | |
2735 | } | |
2736 | else if (ref_fd == -1) | |
2737 | *fd = -1; /* reset to default destination */ | |
2738 | else | |
2739 | { | |
2740 | *fd = _dup (ref_fd); | |
2741 | if (*fd < 0) | |
2742 | return -1; | |
2743 | } | |
2744 | /* _open just sets a flag for O_APPEND, which won't be passed to the | |
2745 | inferior, so we need to actually move the file pointer. */ | |
2746 | if ((mode & O_APPEND) != 0) | |
2747 | _lseek (*fd, 0L, SEEK_END); | |
2748 | return 0; | |
2749 | } | |
2750 | ||
2751 | /* Canonicalize a single redirection spec and set up the corresponding | |
2752 | file descriptor as specified. */ | |
2753 | static int | |
2754 | redir_set_redirection (const char *s, int *inp, int *out, int *err) | |
2755 | { | |
2756 | char buf[__PMAX + 2 + 5]; /* extra space for quotes & redirection string */ | |
2757 | char *d = buf; | |
2758 | const char *start = s; | |
2759 | int quote = 0; | |
2760 | ||
2761 | *d++ = *s++; /* copy the 1st character, < or > or a digit */ | |
2762 | if ((*start == '>' || *start == '1' || *start == '2') | |
2763 | && *s == '>') | |
2764 | { | |
2765 | *d++ = *s++; | |
2766 | if (*s == '>' && *start != '>') | |
2767 | *d++ = *s++; | |
2768 | } | |
2769 | else if (*start == '0' && *s == '<') | |
2770 | *d++ = *s++; | |
2771 | /* cmd.exe recognizes "&N" only immediately after the redirection symbol. */ | |
2772 | if (*s != '&') | |
2773 | { | |
2774 | while (isspace (*s)) /* skip whitespace before file name */ | |
2775 | s++; | |
2776 | *d++ = ' '; /* separate file name with a single space */ | |
2777 | } | |
2778 | ||
2779 | /* Copy the file name. */ | |
2780 | while (*s) | |
2781 | { | |
2782 | /* Remove quoting characters from the file name in buf[]. */ | |
2783 | if (*s == '"') /* could support '..' quoting here */ | |
2784 | { | |
2785 | if (!quote) | |
2786 | quote = *s++; | |
2787 | else if (*s == quote) | |
2788 | { | |
2789 | quote = 0; | |
2790 | s++; | |
2791 | } | |
2792 | else | |
2793 | *d++ = *s++; | |
2794 | } | |
2795 | else if (*s == '\\') | |
2796 | { | |
2797 | if (s[1] == '"') /* could support '..' here */ | |
2798 | s++; | |
2799 | *d++ = *s++; | |
2800 | } | |
2801 | else if (isspace (*s) && !quote) | |
2802 | break; | |
2803 | else | |
2804 | *d++ = *s++; | |
2805 | if (d - buf >= sizeof (buf) - 1) | |
2806 | { | |
2807 | errno = ENAMETOOLONG; | |
2808 | return 0; | |
2809 | } | |
2810 | } | |
2811 | *d = '\0'; | |
2812 | ||
2813 | /* Windows doesn't allow redirection characters in file names, so we | |
2814 | can bail out early if they use them, or if there's no target file | |
2815 | name after the redirection symbol. */ | |
2816 | if (d[-1] == '>' || d[-1] == '<') | |
2817 | { | |
2818 | errno = ENOENT; | |
2819 | return 0; | |
2820 | } | |
2821 | if (redir_open (buf, inp, out, err) == 0) | |
2822 | return s - start; | |
2823 | return 0; | |
2824 | } | |
2825 | ||
2826 | /* Parse the command line for redirection specs and prepare the file | |
2827 | descriptors for the 3 standard streams accordingly. */ | |
2828 | static bool | |
2829 | redirect_inferior_handles (const char *cmd_orig, char *cmd, | |
2830 | int *inp, int *out, int *err) | |
2831 | { | |
2832 | const char *s = cmd_orig; | |
2833 | char *d = cmd; | |
2834 | int quote = 0; | |
2835 | bool retval = false; | |
2836 | ||
2837 | while (isspace (*s)) | |
2838 | *d++ = *s++; | |
2839 | ||
2840 | while (*s) | |
2841 | { | |
2842 | if (*s == '"') /* could also support '..' quoting here */ | |
2843 | { | |
2844 | if (!quote) | |
2845 | quote = *s; | |
2846 | else if (*s == quote) | |
2847 | quote = 0; | |
2848 | } | |
2849 | else if (*s == '\\') | |
2850 | { | |
2851 | if (s[1] == '"') /* escaped quote char */ | |
2852 | s++; | |
2853 | } | |
2854 | else if (!quote) | |
2855 | { | |
2856 | /* Process a single redirection candidate. */ | |
2857 | if (*s == '<' || *s == '>' | |
2858 | || ((*s == '1' || *s == '2') && s[1] == '>') | |
2859 | || (*s == '0' && s[1] == '<')) | |
2860 | { | |
2861 | int skip = redir_set_redirection (s, inp, out, err); | |
2862 | ||
2863 | if (skip <= 0) | |
2864 | return false; | |
2865 | retval = true; | |
2866 | s += skip; | |
2867 | } | |
2868 | } | |
2869 | if (*s) | |
2870 | *d++ = *s++; | |
2871 | } | |
2872 | *d = '\0'; | |
2873 | return retval; | |
2874 | } | |
2875 | #endif /* !__CYGWIN__ */ | |
2876 | ||
dc05df57 | 2877 | /* Start an inferior windows child process and sets inferior_ptid to its pid. |
24e60978 SC |
2878 | EXEC_FILE is the file to run. |
2879 | ALLARGS is a string containing the arguments to the program. | |
2880 | ENV is the environment vector to pass. Errors reported with error(). */ | |
2881 | ||
f6ac5f3d PA |
2882 | void |
2883 | windows_nat_target::create_inferior (const char *exec_file, | |
2884 | const std::string &origallargs, | |
2885 | char **in_env, int from_tty) | |
24e60978 | 2886 | { |
b3c613f2 | 2887 | STARTUPINFO si; |
41b4aadc | 2888 | #ifdef __CYGWIN__ |
b3c613f2 CF |
2889 | cygwin_buf_t real_path[__PMAX]; |
2890 | cygwin_buf_t shell[__PMAX]; /* Path to shell */ | |
d092c5a2 | 2891 | cygwin_buf_t infcwd[__PMAX]; |
d0d0ab16 | 2892 | const char *sh; |
b3c613f2 CF |
2893 | cygwin_buf_t *toexec; |
2894 | cygwin_buf_t *cygallargs; | |
2895 | cygwin_buf_t *args; | |
b7ff339d CV |
2896 | char **old_env = NULL; |
2897 | PWCHAR w32_env; | |
d0d0ab16 | 2898 | size_t len; |
2becadee CF |
2899 | int tty; |
2900 | int ostdin, ostdout, ostderr; | |
8ba42bc5 | 2901 | #else /* !__CYGWIN__ */ |
b3c613f2 | 2902 | char shell[__PMAX]; /* Path to shell */ |
5430098f | 2903 | const char *toexec; |
8ba42bc5 EZ |
2904 | char *args, *allargs_copy; |
2905 | size_t args_len, allargs_len; | |
2906 | int fd_inp = -1, fd_out = -1, fd_err = -1; | |
2907 | HANDLE tty = INVALID_HANDLE_VALUE; | |
8ba42bc5 | 2908 | bool redirected = false; |
c93dbcba EZ |
2909 | char *w32env; |
2910 | char *temp; | |
2911 | size_t envlen; | |
2912 | int i; | |
2913 | size_t envsize; | |
2914 | char **env; | |
8ba42bc5 | 2915 | #endif /* !__CYGWIN__ */ |
096c92dd | 2916 | const char *allargs = origallargs.c_str (); |
d0d0ab16 CV |
2917 | PROCESS_INFORMATION pi; |
2918 | BOOL ret; | |
2919 | DWORD flags = 0; | |
3cb3b8df | 2920 | const char *inferior_io_terminal = get_inferior_io_terminal (); |
24e60978 SC |
2921 | |
2922 | if (!exec_file) | |
8a3fe4f8 | 2923 | error (_("No executable specified, use `target exec'.")); |
24e60978 | 2924 | |
d092c5a2 SDJ |
2925 | const char *inferior_cwd = get_inferior_cwd (); |
2926 | std::string expanded_infcwd; | |
2927 | if (inferior_cwd != NULL) | |
2928 | { | |
2929 | expanded_infcwd = gdb_tilde_expand (inferior_cwd); | |
2930 | /* Mirror slashes on inferior's cwd. */ | |
2931 | std::replace (expanded_infcwd.begin (), expanded_infcwd.end (), | |
2932 | '/', '\\'); | |
2933 | inferior_cwd = expanded_infcwd.c_str (); | |
2934 | } | |
2935 | ||
24e60978 SC |
2936 | memset (&si, 0, sizeof (si)); |
2937 | si.cb = sizeof (si); | |
2938 | ||
d0d0ab16 CV |
2939 | if (new_group) |
2940 | flags |= CREATE_NEW_PROCESS_GROUP; | |
2941 | ||
2942 | if (new_console) | |
cd44747c | 2943 | windows_set_console_info (&si, &flags); |
d0d0ab16 | 2944 | |
10325bc5 | 2945 | #ifdef __CYGWIN__ |
349b409f | 2946 | if (!useshell) |
dfe7f3ac | 2947 | { |
d0d0ab16 CV |
2948 | flags |= DEBUG_ONLY_THIS_PROCESS; |
2949 | if (cygwin_conv_path (CCP_POSIX_TO_WIN_W, exec_file, real_path, | |
b3c613f2 | 2950 | __PMAX * sizeof (cygwin_buf_t)) < 0) |
d0d0ab16 | 2951 | error (_("Error starting executable: %d"), errno); |
dfe7f3ac | 2952 | toexec = real_path; |
b3c613f2 | 2953 | #ifdef __USEWIDE |
d0d0ab16 CV |
2954 | len = mbstowcs (NULL, allargs, 0) + 1; |
2955 | if (len == (size_t) -1) | |
2956 | error (_("Error starting executable: %d"), errno); | |
2957 | cygallargs = (wchar_t *) alloca (len * sizeof (wchar_t)); | |
2958 | mbstowcs (cygallargs, allargs, len); | |
8ba42bc5 | 2959 | #else /* !__USEWIDE */ |
60c5c021 | 2960 | cygallargs = allargs; |
b3c613f2 | 2961 | #endif |
dfe7f3ac CF |
2962 | } |
2963 | else | |
2964 | { | |
974e6844 | 2965 | sh = get_shell (); |
b3c613f2 | 2966 | if (cygwin_conv_path (CCP_POSIX_TO_WIN_W, sh, shell, __PMAX) < 0) |
d0d0ab16 | 2967 | error (_("Error starting executable via shell: %d"), errno); |
b3c613f2 | 2968 | #ifdef __USEWIDE |
d0d0ab16 CV |
2969 | len = sizeof (L" -c 'exec '") + mbstowcs (NULL, exec_file, 0) |
2970 | + mbstowcs (NULL, allargs, 0) + 2; | |
2971 | cygallargs = (wchar_t *) alloca (len * sizeof (wchar_t)); | |
2972 | swprintf (cygallargs, len, L" -c 'exec %s %s'", exec_file, allargs); | |
8ba42bc5 | 2973 | #else /* !__USEWIDE */ |
08850b56 PM |
2974 | len = (sizeof (" -c 'exec '") + strlen (exec_file) |
2975 | + strlen (allargs) + 2); | |
2976 | cygallargs = (char *) alloca (len); | |
2977 | xsnprintf (cygallargs, len, " -c 'exec %s %s'", exec_file, allargs); | |
8ba42bc5 | 2978 | #endif /* __USEWIDE */ |
dfe7f3ac | 2979 | toexec = shell; |
d0d0ab16 | 2980 | flags |= DEBUG_PROCESS; |
dfe7f3ac | 2981 | } |
b3c613f2 | 2982 | |
d092c5a2 SDJ |
2983 | if (inferior_cwd != NULL |
2984 | && cygwin_conv_path (CCP_POSIX_TO_WIN_W, inferior_cwd, | |
2985 | infcwd, strlen (inferior_cwd)) < 0) | |
2986 | error (_("Error converting inferior cwd: %d"), errno); | |
2987 | ||
b3c613f2 CF |
2988 | #ifdef __USEWIDE |
2989 | args = (cygwin_buf_t *) alloca ((wcslen (toexec) + wcslen (cygallargs) + 2) | |
2990 | * sizeof (wchar_t)); | |
d0d0ab16 CV |
2991 | wcscpy (args, toexec); |
2992 | wcscat (args, L" "); | |
2993 | wcscat (args, cygallargs); | |
8ba42bc5 | 2994 | #else /* !__USEWIDE */ |
b3c613f2 CF |
2995 | args = (cygwin_buf_t *) alloca (strlen (toexec) + strlen (cygallargs) + 2); |
2996 | strcpy (args, toexec); | |
2997 | strcat (args, " "); | |
2998 | strcat (args, cygallargs); | |
8ba42bc5 | 2999 | #endif /* !__USEWIDE */ |
b3c613f2 | 3000 | |
b7ff339d CV |
3001 | #ifdef CW_CVT_ENV_TO_WINENV |
3002 | /* First try to create a direct Win32 copy of the POSIX environment. */ | |
3003 | w32_env = (PWCHAR) cygwin_internal (CW_CVT_ENV_TO_WINENV, in_env); | |
3004 | if (w32_env != (PWCHAR) -1) | |
3005 | flags |= CREATE_UNICODE_ENVIRONMENT; | |
3006 | else | |
3007 | /* If that fails, fall back to old method tweaking GDB's environment. */ | |
8ba42bc5 | 3008 | #endif /* CW_CVT_ENV_TO_WINENV */ |
b7ff339d CV |
3009 | { |
3010 | /* Reset all Win32 environment variables to avoid leftover on next run. */ | |
3011 | clear_win32_environment (environ); | |
3012 | /* Prepare the environment vars for CreateProcess. */ | |
3013 | old_env = environ; | |
3014 | environ = in_env; | |
3015 | cygwin_internal (CW_SYNC_WINENV); | |
3016 | w32_env = NULL; | |
3017 | } | |
1750a5ef | 3018 | |
2becadee CF |
3019 | if (!inferior_io_terminal) |
3020 | tty = ostdin = ostdout = ostderr = -1; | |
3021 | else | |
3022 | { | |
3023 | tty = open (inferior_io_terminal, O_RDWR | O_NOCTTY); | |
3024 | if (tty < 0) | |
3025 | { | |
3026 | print_sys_errmsg (inferior_io_terminal, errno); | |
3027 | ostdin = ostdout = ostderr = -1; | |
3028 | } | |
3029 | else | |
3030 | { | |
3031 | ostdin = dup (0); | |
3032 | ostdout = dup (1); | |
3033 | ostderr = dup (2); | |
3034 | dup2 (tty, 0); | |
3035 | dup2 (tty, 1); | |
3036 | dup2 (tty, 2); | |
3037 | } | |
3038 | } | |
d0d0ab16 CV |
3039 | |
3040 | windows_init_thread_list (); | |
b3c613f2 CF |
3041 | ret = CreateProcess (0, |
3042 | args, /* command line */ | |
3043 | NULL, /* Security */ | |
3044 | NULL, /* thread */ | |
3045 | TRUE, /* inherit handles */ | |
3046 | flags, /* start flags */ | |
b7ff339d | 3047 | w32_env, /* environment */ |
d092c5a2 SDJ |
3048 | inferior_cwd != NULL ? infcwd : NULL, /* current |
3049 | directory */ | |
b3c613f2 CF |
3050 | &si, |
3051 | &pi); | |
b7ff339d CV |
3052 | if (w32_env) |
3053 | /* Just free the Win32 environment, if it could be created. */ | |
3054 | free (w32_env); | |
3055 | else | |
3056 | { | |
3057 | /* Reset all environment variables to avoid leftover on next run. */ | |
3058 | clear_win32_environment (in_env); | |
3059 | /* Restore normal GDB environment variables. */ | |
3060 | environ = old_env; | |
3061 | cygwin_internal (CW_SYNC_WINENV); | |
3062 | } | |
3063 | ||
d0d0ab16 CV |
3064 | if (tty >= 0) |
3065 | { | |
6af79d7b | 3066 | ::close (tty); |
d0d0ab16 CV |
3067 | dup2 (ostdin, 0); |
3068 | dup2 (ostdout, 1); | |
3069 | dup2 (ostderr, 2); | |
6af79d7b JT |
3070 | ::close (ostdin); |
3071 | ::close (ostdout); | |
3072 | ::close (ostderr); | |
d0d0ab16 | 3073 | } |
8ba42bc5 EZ |
3074 | #else /* !__CYGWIN__ */ |
3075 | allargs_len = strlen (allargs); | |
3076 | allargs_copy = strcpy ((char *) alloca (allargs_len + 1), allargs); | |
3077 | if (strpbrk (allargs_copy, "<>") != NULL) | |
3078 | { | |
3079 | int e = errno; | |
3080 | errno = 0; | |
3081 | redirected = | |
3082 | redirect_inferior_handles (allargs, allargs_copy, | |
3083 | &fd_inp, &fd_out, &fd_err); | |
3084 | if (errno) | |
6d91ce9a | 3085 | warning (_("Error in redirection: %s."), safe_strerror (errno)); |
8ba42bc5 EZ |
3086 | else |
3087 | errno = e; | |
3088 | allargs_len = strlen (allargs_copy); | |
3089 | } | |
3090 | /* If not all the standard streams are redirected by the command | |
3091 | line, use inferior_io_terminal for those which aren't. */ | |
3092 | if (inferior_io_terminal | |
3093 | && !(fd_inp >= 0 && fd_out >= 0 && fd_err >= 0)) | |
41b4aadc CF |
3094 | { |
3095 | SECURITY_ATTRIBUTES sa; | |
3096 | sa.nLength = sizeof(sa); | |
3097 | sa.lpSecurityDescriptor = 0; | |
3098 | sa.bInheritHandle = TRUE; | |
3099 | tty = CreateFileA (inferior_io_terminal, GENERIC_READ | GENERIC_WRITE, | |
3100 | 0, &sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); | |
3101 | if (tty == INVALID_HANDLE_VALUE) | |
3102 | warning (_("Warning: Failed to open TTY %s, error %#x."), | |
3103 | inferior_io_terminal, (unsigned) GetLastError ()); | |
8ba42bc5 EZ |
3104 | } |
3105 | if (redirected || tty != INVALID_HANDLE_VALUE) | |
3106 | { | |
3107 | if (fd_inp >= 0) | |
3108 | si.hStdInput = (HANDLE) _get_osfhandle (fd_inp); | |
3109 | else if (tty != INVALID_HANDLE_VALUE) | |
3110 | si.hStdInput = tty; | |
41b4aadc | 3111 | else |
8ba42bc5 EZ |
3112 | si.hStdInput = GetStdHandle (STD_INPUT_HANDLE); |
3113 | if (fd_out >= 0) | |
3114 | si.hStdOutput = (HANDLE) _get_osfhandle (fd_out); | |
3115 | else if (tty != INVALID_HANDLE_VALUE) | |
3116 | si.hStdOutput = tty; | |
3117 | else | |
3118 | si.hStdOutput = GetStdHandle (STD_OUTPUT_HANDLE); | |
3119 | if (fd_err >= 0) | |
3120 | si.hStdError = (HANDLE) _get_osfhandle (fd_err); | |
3121 | else if (tty != INVALID_HANDLE_VALUE) | |
3122 | si.hStdError = tty; | |
3123 | else | |
3124 | si.hStdError = GetStdHandle (STD_ERROR_HANDLE); | |
3125 | si.dwFlags |= STARTF_USESTDHANDLES; | |
41b4aadc | 3126 | } |
2becadee | 3127 | |
8ba42bc5 EZ |
3128 | toexec = exec_file; |
3129 | /* Build the command line, a space-separated list of tokens where | |
3130 | the first token is the name of the module to be executed. | |
3131 | To avoid ambiguities introduced by spaces in the module name, | |
3132 | we quote it. */ | |
3133 | args_len = strlen (toexec) + 2 /* quotes */ + allargs_len + 2; | |
3134 | args = (char *) alloca (args_len); | |
3135 | xsnprintf (args, args_len, "\"%s\" %s", toexec, allargs_copy); | |
3136 | ||
3137 | flags |= DEBUG_ONLY_THIS_PROCESS; | |
3138 | ||
c93dbcba EZ |
3139 | /* CreateProcess takes the environment list as a null terminated set of |
3140 | strings (i.e. two nulls terminate the list). */ | |
3141 | ||
3142 | /* Get total size for env strings. */ | |
3143 | for (envlen = 0, i = 0; in_env[i] && *in_env[i]; i++) | |
3144 | envlen += strlen (in_env[i]) + 1; | |
3145 | ||
3146 | envsize = sizeof (in_env[0]) * (i + 1); | |
3147 | env = (char **) alloca (envsize); | |
3148 | memcpy (env, in_env, envsize); | |
3149 | /* Windows programs expect the environment block to be sorted. */ | |
3150 | qsort (env, i, sizeof (char *), envvar_cmp); | |
3151 | ||
0ae1c716 | 3152 | w32env = (char *) alloca (envlen + 1); |
c93dbcba EZ |
3153 | |
3154 | /* Copy env strings into new buffer. */ | |
3155 | for (temp = w32env, i = 0; env[i] && *env[i]; i++) | |
3156 | { | |
3157 | strcpy (temp, env[i]); | |
3158 | temp += strlen (temp) + 1; | |
3159 | } | |
3160 | ||
3161 | /* Final nil string to terminate new env. */ | |
3162 | *temp = 0; | |
3163 | ||
dc05df57 | 3164 | windows_init_thread_list (); |
d0d0ab16 CV |
3165 | ret = CreateProcessA (0, |
3166 | args, /* command line */ | |
3167 | NULL, /* Security */ | |
3168 | NULL, /* thread */ | |
3169 | TRUE, /* inherit handles */ | |
3170 | flags, /* start flags */ | |
c93dbcba | 3171 | w32env, /* environment */ |
d092c5a2 | 3172 | inferior_cwd, /* current directory */ |
d0d0ab16 CV |
3173 | &si, |
3174 | &pi); | |
41b4aadc CF |
3175 | if (tty != INVALID_HANDLE_VALUE) |
3176 | CloseHandle (tty); | |
8ba42bc5 EZ |
3177 | if (fd_inp >= 0) |
3178 | _close (fd_inp); | |
3179 | if (fd_out >= 0) | |
3180 | _close (fd_out); | |
3181 | if (fd_err >= 0) | |
3182 | _close (fd_err); | |
3183 | #endif /* !__CYGWIN__ */ | |
2becadee | 3184 | |
24e60978 | 3185 | if (!ret) |
d50a0ce2 | 3186 | error (_("Error creating process %s, (error %u)."), |
8a3fe4f8 | 3187 | exec_file, (unsigned) GetLastError ()); |
24e60978 | 3188 | |
46f9f931 HD |
3189 | #ifdef __x86_64__ |
3190 | BOOL wow64; | |
3191 | if (IsWow64Process (pi.hProcess, &wow64)) | |
3192 | wow64_process = wow64; | |
3193 | #endif | |
3194 | ||
c1766e7d PM |
3195 | CloseHandle (pi.hThread); |
3196 | CloseHandle (pi.hProcess); | |
3197 | ||
dfe7f3ac CF |
3198 | if (useshell && shell[0] != '\0') |
3199 | saw_create = -1; | |
3200 | else | |
3201 | saw_create = 0; | |
3202 | ||
f6ac5f3d | 3203 | do_initial_windows_stuff (this, pi.dwProcessId, 0); |
d3a09475 | 3204 | |
17617f2d | 3205 | /* windows_continue (DBG_CONTINUE, -1, 0); */ |
24e60978 SC |
3206 | } |
3207 | ||
f6ac5f3d PA |
3208 | void |
3209 | windows_nat_target::mourn_inferior () | |
24e60978 | 3210 | { |
17617f2d | 3211 | (void) windows_continue (DBG_CONTINUE, -1, 0); |
df7e5265 | 3212 | x86_cleanup_dregs(); |
bf25528d CF |
3213 | if (open_process_used) |
3214 | { | |
3215 | CHECK (CloseHandle (current_process_handle)); | |
3216 | open_process_used = 0; | |
3217 | } | |
7928d571 | 3218 | siginfo_er.ExceptionCode = 0; |
f6ac5f3d | 3219 | inf_child_target::mourn_inferior (); |
24e60978 SC |
3220 | } |
3221 | ||
24e60978 | 3222 | /* Send a SIGINT to the process group. This acts just like the user typed a |
581e13c1 | 3223 | ^C on the controlling terminal. */ |
24e60978 | 3224 | |
f6ac5f3d PA |
3225 | void |
3226 | windows_nat_target::interrupt () | |
24e60978 | 3227 | { |
1ef980b9 | 3228 | DEBUG_EVENTS (("gdb: GenerateConsoleCtrlEvent (CTRLC_EVENT, 0)\n")); |
1e37c281 | 3229 | CHECK (GenerateConsoleCtrlEvent (CTRL_C_EVENT, current_event.dwProcessId)); |
3a4b77d8 | 3230 | registers_changed (); /* refresh register state */ |
24e60978 SC |
3231 | } |
3232 | ||
44f38867 PA |
3233 | /* Helper for windows_xfer_partial that handles memory transfers. |
3234 | Arguments are like target_xfer_partial. */ | |
3235 | ||
9b409511 | 3236 | static enum target_xfer_status |
44f38867 | 3237 | windows_xfer_memory (gdb_byte *readbuf, const gdb_byte *writebuf, |
9b409511 | 3238 | ULONGEST memaddr, ULONGEST len, ULONGEST *xfered_len) |
24e60978 | 3239 | { |
5732a500 | 3240 | SIZE_T done = 0; |
44f38867 | 3241 | BOOL success; |
9e52adf9 | 3242 | DWORD lasterror = 0; |
44f38867 PA |
3243 | |
3244 | if (writebuf != NULL) | |
24e60978 | 3245 | { |
a2388568 | 3246 | DEBUG_MEM (("gdb: write target memory, %s bytes at %s\n", |
b55e14c7 | 3247 | pulongest (len), core_addr_to_string (memaddr))); |
44f38867 PA |
3248 | success = WriteProcessMemory (current_process_handle, |
3249 | (LPVOID) (uintptr_t) memaddr, writebuf, | |
3250 | len, &done); | |
9e52adf9 | 3251 | if (!success) |
7126d5c8 | 3252 | lasterror = GetLastError (); |
2b008701 | 3253 | FlushInstructionCache (current_process_handle, |
2c647436 | 3254 | (LPCVOID) (uintptr_t) memaddr, len); |
24e60978 SC |
3255 | } |
3256 | else | |
3257 | { | |
a2388568 | 3258 | DEBUG_MEM (("gdb: read target memory, %s bytes at %s\n", |
b55e14c7 | 3259 | pulongest (len), core_addr_to_string (memaddr))); |
44f38867 PA |
3260 | success = ReadProcessMemory (current_process_handle, |
3261 | (LPCVOID) (uintptr_t) memaddr, readbuf, | |
3262 | len, &done); | |
9e52adf9 | 3263 | if (!success) |
7126d5c8 | 3264 | lasterror = GetLastError (); |
24e60978 | 3265 | } |
9b409511 | 3266 | *xfered_len = (ULONGEST) done; |
9e52adf9 | 3267 | if (!success && lasterror == ERROR_PARTIAL_COPY && done > 0) |
9b409511 | 3268 | return TARGET_XFER_OK; |
9e52adf9 | 3269 | else |
9b409511 | 3270 | return success ? TARGET_XFER_OK : TARGET_XFER_E_IO; |
24e60978 SC |
3271 | } |
3272 | ||
f6ac5f3d PA |
3273 | void |
3274 | windows_nat_target::kill () | |
24e60978 | 3275 | { |
3cee93ac CF |
3276 | CHECK (TerminateProcess (current_process_handle, 0)); |
3277 | ||
b5edcb45 ILT |
3278 | for (;;) |
3279 | { | |
17617f2d | 3280 | if (!windows_continue (DBG_CONTINUE, -1, 1)) |
b5edcb45 | 3281 | break; |
0a4afda3 | 3282 | if (!wait_for_debug_event (¤t_event, INFINITE)) |
b5edcb45 | 3283 | break; |
3cee93ac | 3284 | if (current_event.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT) |
b5edcb45 ILT |
3285 | break; |
3286 | } | |
3287 | ||
9eee20eb | 3288 | target_mourn_inferior (inferior_ptid); /* Or just windows_mourn_inferior? */ |
24e60978 SC |
3289 | } |
3290 | ||
f6ac5f3d PA |
3291 | void |
3292 | windows_nat_target::close () | |
24e60978 | 3293 | { |
dc05df57 | 3294 | DEBUG_EVENTS (("gdb: windows_close, inferior_ptid=%d\n", |
e99b03dc | 3295 | inferior_ptid.pid ())); |
24e60978 | 3296 | } |
1ef980b9 | 3297 | |
581e13c1 | 3298 | /* Convert pid to printable format. */ |
a068643d | 3299 | std::string |
f6ac5f3d | 3300 | windows_nat_target::pid_to_str (ptid_t ptid) |
24e60978 | 3301 | { |
7c7411bc TT |
3302 | if (ptid.lwp () != 0) |
3303 | return string_printf ("Thread %d.0x%lx", ptid.pid (), ptid.lwp ()); | |
2dc38344 PA |
3304 | |
3305 | return normal_pid_to_str (ptid); | |
3ee6f623 CF |
3306 | } |
3307 | ||
9b409511 | 3308 | static enum target_xfer_status |
dc05df57 | 3309 | windows_xfer_shared_libraries (struct target_ops *ops, |
9b409511 YQ |
3310 | enum target_object object, const char *annex, |
3311 | gdb_byte *readbuf, const gdb_byte *writebuf, | |
3312 | ULONGEST offset, ULONGEST len, | |
3313 | ULONGEST *xfered_len) | |
3cb8e7f6 | 3314 | { |
de1b3c3d PA |
3315 | struct obstack obstack; |
3316 | const char *buf; | |
3317 | LONGEST len_avail; | |
3cb8e7f6 | 3318 | struct so_list *so; |
3cb8e7f6 | 3319 | |
de1b3c3d | 3320 | if (writebuf) |
2ed4b548 | 3321 | return TARGET_XFER_E_IO; |
3cb8e7f6 | 3322 | |
de1b3c3d PA |
3323 | obstack_init (&obstack); |
3324 | obstack_grow_str (&obstack, "<library-list>\n"); | |
3325 | for (so = solib_start.next; so; so = so->next) | |
d0e449a1 SM |
3326 | { |
3327 | lm_info_windows *li = (lm_info_windows *) so->lm_info; | |
3328 | ||
3329 | windows_xfer_shared_library (so->so_name, (CORE_ADDR) | |
3330 | (uintptr_t) li->load_addr, | |
c162ed3e | 3331 | &li->text_offset, |
d0e449a1 SM |
3332 | target_gdbarch (), &obstack); |
3333 | } | |
de1b3c3d | 3334 | obstack_grow_str0 (&obstack, "</library-list>\n"); |
3cb8e7f6 | 3335 | |
0ae1c716 | 3336 | buf = (const char *) obstack_finish (&obstack); |
de1b3c3d PA |
3337 | len_avail = strlen (buf); |
3338 | if (offset >= len_avail) | |
49dc7f4b PM |
3339 | len= 0; |
3340 | else | |
3341 | { | |
3342 | if (len > len_avail - offset) | |
3343 | len = len_avail - offset; | |
3344 | memcpy (readbuf, buf + offset, len); | |
3345 | } | |
3cb8e7f6 | 3346 | |
de1b3c3d | 3347 | obstack_free (&obstack, NULL); |
9b409511 | 3348 | *xfered_len = (ULONGEST) len; |
0837c976 | 3349 | return len != 0 ? TARGET_XFER_OK : TARGET_XFER_EOF; |
3cb8e7f6 CF |
3350 | } |
3351 | ||
7928d571 HD |
3352 | /* Helper for windows_nat_target::xfer_partial that handles signal info. */ |
3353 | ||
3354 | static enum target_xfer_status | |
3355 | windows_xfer_siginfo (gdb_byte *readbuf, ULONGEST offset, ULONGEST len, | |
3356 | ULONGEST *xfered_len) | |
3357 | { | |
46f9f931 HD |
3358 | char *buf = (char *) &siginfo_er; |
3359 | size_t bufsize = sizeof (siginfo_er); | |
3360 | ||
3361 | #ifdef __x86_64__ | |
3362 | EXCEPTION_RECORD32 er32; | |
3363 | if (wow64_process) | |
3364 | { | |
3365 | buf = (char *) &er32; | |
3366 | bufsize = sizeof (er32); | |
3367 | ||
3368 | er32.ExceptionCode = siginfo_er.ExceptionCode; | |
3369 | er32.ExceptionFlags = siginfo_er.ExceptionFlags; | |
3370 | er32.ExceptionRecord = (uintptr_t) siginfo_er.ExceptionRecord; | |
3371 | er32.ExceptionAddress = (uintptr_t) siginfo_er.ExceptionAddress; | |
3372 | er32.NumberParameters = siginfo_er.NumberParameters; | |
3373 | int i; | |
3374 | for (i = 0; i < EXCEPTION_MAXIMUM_PARAMETERS; i++) | |
3375 | er32.ExceptionInformation[i] = siginfo_er.ExceptionInformation[i]; | |
3376 | } | |
3377 | #endif | |
3378 | ||
7928d571 HD |
3379 | if (siginfo_er.ExceptionCode == 0) |
3380 | return TARGET_XFER_E_IO; | |
3381 | ||
3382 | if (readbuf == nullptr) | |
3383 | return TARGET_XFER_E_IO; | |
3384 | ||
46f9f931 | 3385 | if (offset > bufsize) |
7928d571 HD |
3386 | return TARGET_XFER_E_IO; |
3387 | ||
46f9f931 HD |
3388 | if (offset + len > bufsize) |
3389 | len = bufsize - offset; | |
7928d571 | 3390 | |
46f9f931 | 3391 | memcpy (readbuf, buf + offset, len); |
7928d571 HD |
3392 | *xfered_len = len; |
3393 | ||
3394 | return TARGET_XFER_OK; | |
3395 | } | |
3396 | ||
f6ac5f3d PA |
3397 | enum target_xfer_status |
3398 | windows_nat_target::xfer_partial (enum target_object object, | |
3399 | const char *annex, gdb_byte *readbuf, | |
2f4f025f TT |
3400 | const gdb_byte *writebuf, ULONGEST offset, |
3401 | ULONGEST len, ULONGEST *xfered_len) | |
3cb8e7f6 | 3402 | { |
de1b3c3d | 3403 | switch (object) |
3cb8e7f6 | 3404 | { |
de1b3c3d | 3405 | case TARGET_OBJECT_MEMORY: |
9b409511 | 3406 | return windows_xfer_memory (readbuf, writebuf, offset, len, xfered_len); |
de1b3c3d PA |
3407 | |
3408 | case TARGET_OBJECT_LIBRARIES: | |
f6ac5f3d | 3409 | return windows_xfer_shared_libraries (this, object, annex, readbuf, |
9b409511 | 3410 | writebuf, offset, len, xfered_len); |
3929abe9 | 3411 | |
7928d571 HD |
3412 | case TARGET_OBJECT_SIGNAL_INFO: |
3413 | return windows_xfer_siginfo (readbuf, offset, len, xfered_len); | |
3414 | ||
de1b3c3d | 3415 | default: |
2f4f025f | 3416 | if (beneath () == NULL) |
178d6a63 JB |
3417 | { |
3418 | /* This can happen when requesting the transfer of unsupported | |
3419 | objects before a program has been started (and therefore | |
3420 | with the current_target having no target beneath). */ | |
3421 | return TARGET_XFER_E_IO; | |
3422 | } | |
2f4f025f TT |
3423 | return beneath ()->xfer_partial (object, annex, |
3424 | readbuf, writebuf, offset, len, | |
3425 | xfered_len); | |
3929abe9 | 3426 | } |
02c5aecd CF |
3427 | } |
3428 | ||
711e434b PM |
3429 | /* Provide thread local base, i.e. Thread Information Block address. |
3430 | Returns 1 if ptid is found and sets *ADDR to thread_local_base. */ | |
3431 | ||
57810aa7 | 3432 | bool |
f6ac5f3d | 3433 | windows_nat_target::get_tib_address (ptid_t ptid, CORE_ADDR *addr) |
711e434b | 3434 | { |
876d1cd7 | 3435 | windows_thread_info *th; |
711e434b | 3436 | |
28688adf | 3437 | th = thread_rec (ptid, DONT_INVALIDATE_CONTEXT); |
711e434b | 3438 | if (th == NULL) |
57810aa7 | 3439 | return false; |
711e434b PM |
3440 | |
3441 | if (addr != NULL) | |
3442 | *addr = th->thread_local_base; | |
3443 | ||
57810aa7 | 3444 | return true; |
711e434b PM |
3445 | } |
3446 | ||
f6ac5f3d PA |
3447 | ptid_t |
3448 | windows_nat_target::get_ada_task_ptid (long lwp, long thread) | |
1e2f1c5c | 3449 | { |
7c7411bc | 3450 | return ptid_t (inferior_ptid.pid (), lwp, 0); |
1e2f1c5c JB |
3451 | } |
3452 | ||
24cdb46e РИ |
3453 | /* Implementation of the to_thread_name method. */ |
3454 | ||
f6ac5f3d PA |
3455 | const char * |
3456 | windows_nat_target::thread_name (struct thread_info *thr) | |
24cdb46e | 3457 | { |
28688adf | 3458 | return thread_rec (thr->ptid, DONT_INVALIDATE_CONTEXT)->name.get (); |
24cdb46e РИ |
3459 | } |
3460 | ||
24e60978 | 3461 | |
6c265988 | 3462 | void _initialize_windows_nat (); |
24e60978 | 3463 | void |
6c265988 | 3464 | _initialize_windows_nat () |
24e60978 | 3465 | { |
df7e5265 GB |
3466 | x86_dr_low.set_control = cygwin_set_dr7; |
3467 | x86_dr_low.set_addr = cygwin_set_dr; | |
3468 | x86_dr_low.get_addr = cygwin_get_dr; | |
3469 | x86_dr_low.get_status = cygwin_get_dr6; | |
3470 | x86_dr_low.get_control = cygwin_get_dr7; | |
51a9c8c5 | 3471 | |
df7e5265 GB |
3472 | /* x86_dr_low.debug_register_length field is set by |
3473 | calling x86_set_debug_register_length function | |
51a9c8c5 | 3474 | in processor windows specific native file. */ |
fa58ee11 | 3475 | |
d9f719f1 | 3476 | add_inf_child_target (&the_windows_nat_target); |
1ef980b9 | 3477 | |
d0d0ab16 CV |
3478 | #ifdef __CYGWIN__ |
3479 | cygwin_internal (CW_SET_DOS_FILE_WARNING, 0); | |
3480 | #endif | |
3481 | ||
463888ab РИ |
3482 | add_com ("signal-event", class_run, signal_event_command, _("\ |
3483 | Signal a crashed process with event ID, to allow its debugging.\n\ | |
3484 | This command is needed in support of setting up GDB as JIT debugger on \ | |
3485 | MS-Windows. The command should be invoked from the GDB command line using \ | |
3486 | the '-ex' command-line option. The ID of the event that blocks the \ | |
3487 | crashed process will be supplied by the Windows JIT debugging mechanism.")); | |
3488 | ||
10325bc5 | 3489 | #ifdef __CYGWIN__ |
5bf193a2 AC |
3490 | add_setshow_boolean_cmd ("shell", class_support, &useshell, _("\ |
3491 | Set use of shell to start subprocess."), _("\ | |
3492 | Show use of shell to start subprocess."), NULL, | |
3493 | NULL, | |
3494 | NULL, /* FIXME: i18n: */ | |
3495 | &setlist, &showlist); | |
3496 | ||
581e13c1 MS |
3497 | add_setshow_boolean_cmd ("cygwin-exceptions", class_support, |
3498 | &cygwin_exceptions, _("\ | |
09280ddf CF |
3499 | Break when an exception is detected in the Cygwin DLL itself."), _("\ |
3500 | Show whether gdb breaks on exceptions in the Cygwin DLL itself."), NULL, | |
3501 | NULL, | |
3502 | NULL, /* FIXME: i18n: */ | |
3503 | &setlist, &showlist); | |
10325bc5 | 3504 | #endif |
09280ddf | 3505 | |
5bf193a2 AC |
3506 | add_setshow_boolean_cmd ("new-console", class_support, &new_console, _("\ |
3507 | Set creation of new console when creating child process."), _("\ | |
3508 | Show creation of new console when creating child process."), NULL, | |
3509 | NULL, | |
3510 | NULL, /* FIXME: i18n: */ | |
3511 | &setlist, &showlist); | |
3512 | ||
3513 | add_setshow_boolean_cmd ("new-group", class_support, &new_group, _("\ | |
3514 | Set creation of new group when creating child process."), _("\ | |
3515 | Show creation of new group when creating child process."), NULL, | |
3516 | NULL, | |
3517 | NULL, /* FIXME: i18n: */ | |
3518 | &setlist, &showlist); | |
3519 | ||
3520 | add_setshow_boolean_cmd ("debugexec", class_support, &debug_exec, _("\ | |
3521 | Set whether to display execution in child process."), _("\ | |
3522 | Show whether to display execution in child process."), NULL, | |
3523 | NULL, | |
3524 | NULL, /* FIXME: i18n: */ | |
3525 | &setlist, &showlist); | |
3526 | ||
3527 | add_setshow_boolean_cmd ("debugevents", class_support, &debug_events, _("\ | |
3528 | Set whether to display kernel events in child process."), _("\ | |
3529 | Show whether to display kernel events in child process."), NULL, | |
3530 | NULL, | |
3531 | NULL, /* FIXME: i18n: */ | |
3532 | &setlist, &showlist); | |
3533 | ||
3534 | add_setshow_boolean_cmd ("debugmemory", class_support, &debug_memory, _("\ | |
3535 | Set whether to display memory accesses in child process."), _("\ | |
3536 | Show whether to display memory accesses in child process."), NULL, | |
3537 | NULL, | |
3538 | NULL, /* FIXME: i18n: */ | |
3539 | &setlist, &showlist); | |
3540 | ||
3541 | add_setshow_boolean_cmd ("debugexceptions", class_support, | |
3542 | &debug_exceptions, _("\ | |
3543 | Set whether to display kernel exceptions in child process."), _("\ | |
3544 | Show whether to display kernel exceptions in child process."), NULL, | |
3545 | NULL, | |
3546 | NULL, /* FIXME: i18n: */ | |
3547 | &setlist, &showlist); | |
1ef980b9 | 3548 | |
711e434b | 3549 | init_w32_command_list (); |
c1748f97 PM |
3550 | |
3551 | add_cmd ("selector", class_info, display_selectors, | |
1a966eab | 3552 | _("Display selectors infos."), |
c1748f97 | 3553 | &info_w32_cmdlist); |
24e60978 | 3554 | } |
3cee93ac | 3555 | |
fa4ba8da PM |
3556 | /* Hardware watchpoint support, adapted from go32-nat.c code. */ |
3557 | ||
3558 | /* Pass the address ADDR to the inferior in the I'th debug register. | |
3559 | Here we just store the address in dr array, the registers will be | |
dc05df57 | 3560 | actually set up when windows_continue is called. */ |
9bb9e8ad | 3561 | static void |
fa4ba8da PM |
3562 | cygwin_set_dr (int i, CORE_ADDR addr) |
3563 | { | |
3564 | if (i < 0 || i > 3) | |
3565 | internal_error (__FILE__, __LINE__, | |
e2e0b3e5 | 3566 | _("Invalid register %d in cygwin_set_dr.\n"), i); |
41b4aadc | 3567 | dr[i] = addr; |
fa4ba8da PM |
3568 | debug_registers_changed = 1; |
3569 | debug_registers_used = 1; | |
3570 | } | |
3571 | ||
3572 | /* Pass the value VAL to the inferior in the DR7 debug control | |
3573 | register. Here we just store the address in D_REGS, the watchpoint | |
dc05df57 | 3574 | will be actually set up in windows_wait. */ |
9bb9e8ad PM |
3575 | static void |
3576 | cygwin_set_dr7 (unsigned long val) | |
fa4ba8da | 3577 | { |
9bb9e8ad | 3578 | dr[7] = (CORE_ADDR) val; |
fa4ba8da PM |
3579 | debug_registers_changed = 1; |
3580 | debug_registers_used = 1; | |
3581 | } | |
3582 | ||
7b50312a PA |
3583 | /* Get the value of debug register I from the inferior. */ |
3584 | ||
3585 | static CORE_ADDR | |
3586 | cygwin_get_dr (int i) | |
3587 | { | |
3588 | return dr[i]; | |
3589 | } | |
3590 | ||
fa4ba8da PM |
3591 | /* Get the value of the DR6 debug status register from the inferior. |
3592 | Here we just return the value stored in dr[6] | |
3593 | by the last call to thread_rec for current_event.dwThreadId id. */ | |
9bb9e8ad | 3594 | static unsigned long |
fa4ba8da PM |
3595 | cygwin_get_dr6 (void) |
3596 | { | |
9bb9e8ad | 3597 | return (unsigned long) dr[6]; |
fa4ba8da PM |
3598 | } |
3599 | ||
7b50312a PA |
3600 | /* Get the value of the DR7 debug status register from the inferior. |
3601 | Here we just return the value stored in dr[7] by the last call to | |
3602 | thread_rec for current_event.dwThreadId id. */ | |
3603 | ||
3604 | static unsigned long | |
3605 | cygwin_get_dr7 (void) | |
3606 | { | |
3607 | return (unsigned long) dr[7]; | |
3608 | } | |
3609 | ||
2dc38344 | 3610 | /* Determine if the thread referenced by "ptid" is alive |
3cee93ac | 3611 | by "polling" it. If WaitForSingleObject returns WAIT_OBJECT_0 |
581e13c1 | 3612 | it means that the thread has died. Otherwise it is assumed to be alive. */ |
f6ac5f3d | 3613 | |
57810aa7 | 3614 | bool |
f6ac5f3d | 3615 | windows_nat_target::thread_alive (ptid_t ptid) |
3cee93ac | 3616 | { |
7c7411bc | 3617 | gdb_assert (ptid.lwp () != 0); |
39f77062 | 3618 | |
28688adf | 3619 | return (WaitForSingleObject (thread_rec (ptid, DONT_INVALIDATE_CONTEXT)->h, 0) |
8e61ebec | 3620 | != WAIT_OBJECT_0); |
3cee93ac CF |
3621 | } |
3622 | ||
6c265988 | 3623 | void _initialize_check_for_gdb_ini (); |
2a3d5645 | 3624 | void |
6c265988 | 3625 | _initialize_check_for_gdb_ini () |
2a3d5645 CF |
3626 | { |
3627 | char *homedir; | |
3628 | if (inhibit_gdbinit) | |
3629 | return; | |
3630 | ||
3631 | homedir = getenv ("HOME"); | |
3632 | if (homedir) | |
3633 | { | |
3634 | char *p; | |
3635 | char *oldini = (char *) alloca (strlen (homedir) + | |
1270fac6 | 3636 | sizeof ("gdb.ini") + 1); |
2a3d5645 CF |
3637 | strcpy (oldini, homedir); |
3638 | p = strchr (oldini, '\0'); | |
0ba1096a | 3639 | if (p > oldini && !IS_DIR_SEPARATOR (p[-1])) |
2a3d5645 CF |
3640 | *p++ = '/'; |
3641 | strcpy (p, "gdb.ini"); | |
3642 | if (access (oldini, 0) == 0) | |
3643 | { | |
3644 | int len = strlen (oldini); | |
1270fac6 | 3645 | char *newini = (char *) alloca (len + 2); |
08850b56 | 3646 | |
1270fac6 | 3647 | xsnprintf (newini, len + 2, "%.*s.gdbinit", |
08850b56 | 3648 | (int) (len - (sizeof ("gdb.ini") - 1)), oldini); |
8a3fe4f8 | 3649 | warning (_("obsolete '%s' found. Rename to '%s'."), oldini, newini); |
2a3d5645 CF |
3650 | } |
3651 | } | |
3652 | } | |
33605d39 | 3653 | |
2b008701 | 3654 | /* Define dummy functions which always return error for the rare cases where |
581e13c1 | 3655 | these functions could not be found. */ |
2b008701 CF |
3656 | static BOOL WINAPI |
3657 | bad_DebugActiveProcessStop (DWORD w) | |
3658 | { | |
3659 | return FALSE; | |
3660 | } | |
3661 | static BOOL WINAPI | |
3662 | bad_DebugBreakProcess (HANDLE w) | |
3663 | { | |
3664 | return FALSE; | |
3665 | } | |
3666 | static BOOL WINAPI | |
3667 | bad_DebugSetProcessKillOnExit (BOOL w) | |
3668 | { | |
3669 | return FALSE; | |
3670 | } | |
3671 | static BOOL WINAPI | |
3672 | bad_EnumProcessModules (HANDLE w, HMODULE *x, DWORD y, LPDWORD z) | |
3673 | { | |
3674 | return FALSE; | |
3675 | } | |
b3c613f2 CF |
3676 | |
3677 | #ifdef __USEWIDE | |
2b008701 | 3678 | static DWORD WINAPI |
b3c613f2 | 3679 | bad_GetModuleFileNameExW (HANDLE w, HMODULE x, LPWSTR y, DWORD z) |
2b008701 CF |
3680 | { |
3681 | return 0; | |
3682 | } | |
d0d0ab16 CV |
3683 | #else |
3684 | static DWORD WINAPI | |
b3c613f2 | 3685 | bad_GetModuleFileNameExA (HANDLE w, HMODULE x, LPSTR y, DWORD z) |
d0d0ab16 CV |
3686 | { |
3687 | return 0; | |
3688 | } | |
3689 | #endif | |
b3c613f2 | 3690 | |
2b008701 CF |
3691 | static BOOL WINAPI |
3692 | bad_GetModuleInformation (HANDLE w, HMODULE x, LPMODULEINFO y, DWORD z) | |
3693 | { | |
3694 | return FALSE; | |
3695 | } | |
3696 | ||
418c6cb3 CF |
3697 | static BOOL WINAPI |
3698 | bad_OpenProcessToken (HANDLE w, DWORD x, PHANDLE y) | |
3699 | { | |
3700 | return FALSE; | |
3701 | } | |
3702 | ||
cd44747c PM |
3703 | static BOOL WINAPI |
3704 | bad_GetCurrentConsoleFont (HANDLE w, BOOL bMaxWindow, CONSOLE_FONT_INFO *f) | |
3705 | { | |
3706 | f->nFont = 0; | |
3707 | return 1; | |
3708 | } | |
3709 | static COORD WINAPI | |
3710 | bad_GetConsoleFontSize (HANDLE w, DWORD nFont) | |
3711 | { | |
3712 | COORD size; | |
3713 | size.X = 8; | |
3714 | size.Y = 12; | |
3715 | return size; | |
3716 | } | |
3717 | ||
2b008701 | 3718 | /* Load any functions which may not be available in ancient versions |
581e13c1 | 3719 | of Windows. */ |
d603d4b3 | 3720 | |
6c265988 | 3721 | void _initialize_loadable (); |
33605d39 | 3722 | void |
6c265988 | 3723 | _initialize_loadable () |
33605d39 | 3724 | { |
2b008701 CF |
3725 | HMODULE hm = NULL; |
3726 | ||
43499ea3 PA |
3727 | #define GPA(m, func) \ |
3728 | func = (func ## _ftype *) GetProcAddress (m, #func) | |
3729 | ||
2b008701 CF |
3730 | hm = LoadLibrary ("kernel32.dll"); |
3731 | if (hm) | |
33605d39 | 3732 | { |
43499ea3 PA |
3733 | GPA (hm, DebugActiveProcessStop); |
3734 | GPA (hm, DebugBreakProcess); | |
3735 | GPA (hm, DebugSetProcessKillOnExit); | |
3736 | GPA (hm, GetConsoleFontSize); | |
3737 | GPA (hm, DebugActiveProcessStop); | |
3738 | GPA (hm, GetCurrentConsoleFont); | |
46f9f931 HD |
3739 | #ifdef __x86_64__ |
3740 | GPA (hm, Wow64SuspendThread); | |
3741 | GPA (hm, Wow64GetThreadContext); | |
3742 | GPA (hm, Wow64SetThreadContext); | |
3743 | GPA (hm, Wow64GetThreadSelectorEntry); | |
3744 | #endif | |
2b008701 | 3745 | } |
33605d39 | 3746 | |
2b008701 | 3747 | /* Set variables to dummy versions of these processes if the function |
581e13c1 | 3748 | wasn't found in kernel32.dll. */ |
b3c613f2 CF |
3749 | if (!DebugBreakProcess) |
3750 | DebugBreakProcess = bad_DebugBreakProcess; | |
3751 | if (!DebugActiveProcessStop || !DebugSetProcessKillOnExit) | |
2b008701 | 3752 | { |
b3c613f2 CF |
3753 | DebugActiveProcessStop = bad_DebugActiveProcessStop; |
3754 | DebugSetProcessKillOnExit = bad_DebugSetProcessKillOnExit; | |
2b008701 | 3755 | } |
cd44747c PM |
3756 | if (!GetConsoleFontSize) |
3757 | GetConsoleFontSize = bad_GetConsoleFontSize; | |
3758 | if (!GetCurrentConsoleFont) | |
3759 | GetCurrentConsoleFont = bad_GetCurrentConsoleFont; | |
33605d39 | 3760 | |
2b008701 | 3761 | /* Load optional functions used for retrieving filename information |
581e13c1 | 3762 | associated with the currently debugged process or its dlls. */ |
2b008701 CF |
3763 | hm = LoadLibrary ("psapi.dll"); |
3764 | if (hm) | |
3765 | { | |
43499ea3 | 3766 | GPA (hm, EnumProcessModules); |
46f9f931 HD |
3767 | #ifdef __x86_64__ |
3768 | GPA (hm, EnumProcessModulesEx); | |
3769 | #endif | |
43499ea3 | 3770 | GPA (hm, GetModuleInformation); |
aec47d1d JB |
3771 | GetModuleFileNameEx = (GetModuleFileNameEx_ftype *) |
3772 | GetProcAddress (hm, GetModuleFileNameEx_name); | |
33605d39 CF |
3773 | } |
3774 | ||
b3c613f2 | 3775 | if (!EnumProcessModules || !GetModuleInformation || !GetModuleFileNameEx) |
2b008701 CF |
3776 | { |
3777 | /* Set variables to dummy versions of these processes if the function | |
581e13c1 | 3778 | wasn't found in psapi.dll. */ |
b3c613f2 CF |
3779 | EnumProcessModules = bad_EnumProcessModules; |
3780 | GetModuleInformation = bad_GetModuleInformation; | |
3781 | GetModuleFileNameEx = bad_GetModuleFileNameEx; | |
581e13c1 MS |
3782 | /* This will probably fail on Windows 9x/Me. Let the user know |
3783 | that we're missing some functionality. */ | |
3784 | warning(_("\ | |
3785 | cannot automatically find executable file or library to read symbols.\n\ | |
3786 | Use \"file\" or \"dll\" command to load executable/libraries directly.")); | |
418c6cb3 CF |
3787 | } |
3788 | ||
3789 | hm = LoadLibrary ("advapi32.dll"); | |
3790 | if (hm) | |
3791 | { | |
43499ea3 PA |
3792 | GPA (hm, OpenProcessToken); |
3793 | GPA (hm, LookupPrivilegeValueA); | |
3794 | GPA (hm, AdjustTokenPrivileges); | |
418c6cb3 | 3795 | /* Only need to set one of these since if OpenProcessToken fails nothing |
581e13c1 MS |
3796 | else is needed. */ |
3797 | if (!OpenProcessToken || !LookupPrivilegeValueA | |
3798 | || !AdjustTokenPrivileges) | |
b3c613f2 | 3799 | OpenProcessToken = bad_OpenProcessToken; |
2b008701 | 3800 | } |
43499ea3 PA |
3801 | |
3802 | #undef GPA | |
33605d39 | 3803 | } |