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