Commit | Line | Data |
---|---|---|
dc05df57 | 1 | /* Target-vector operations for controlling windows child processes, for GDB. |
0a65a603 | 2 | |
ecd75fc8 | 3 | Copyright (C) 1995-2014 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" |
60250e8b | 29 | #include "exceptions.h" |
24e60978 SC |
30 | #include "gdbcore.h" |
31 | #include "command.h" | |
fa58ee11 | 32 | #include "completer.h" |
4e052eda | 33 | #include "regcache.h" |
2a3d5645 | 34 | #include "top.h" |
403d9909 CF |
35 | #include <signal.h> |
36 | #include <sys/types.h> | |
37 | #include <fcntl.h> | |
38 | #include <stdlib.h> | |
39 | #include <windows.h> | |
40 | #include <imagehlp.h> | |
2b008701 | 41 | #include <psapi.h> |
10325bc5 | 42 | #ifdef __CYGWIN__ |
b7ff339d | 43 | #include <wchar.h> |
403d9909 | 44 | #include <sys/cygwin.h> |
b7ff339d | 45 | #include <cygwin/version.h> |
10325bc5 | 46 | #endif |
cad9cd60 | 47 | |
24e60978 | 48 | #include "buildsym.h" |
0ba1096a | 49 | #include "filenames.h" |
1ef980b9 SC |
50 | #include "symfile.h" |
51 | #include "objfiles.h" | |
92107356 | 52 | #include "gdb_bfd.h" |
de1b3c3d | 53 | #include "gdb_obstack.h" |
0e9f083f | 54 | #include <string.h> |
fdfa3315 | 55 | #include "gdbthread.h" |
24e60978 | 56 | #include "gdbcmd.h" |
1e37c281 | 57 | #include <unistd.h> |
4646aa9d | 58 | #include "exec.h" |
3ee6f623 | 59 | #include "solist.h" |
3cb8e7f6 | 60 | #include "solib.h" |
de1b3c3d | 61 | #include "xml-support.h" |
24e60978 | 62 | |
6c7de422 MK |
63 | #include "i386-tdep.h" |
64 | #include "i387-tdep.h" | |
65 | ||
31b060a2 CF |
66 | #include "windows-tdep.h" |
67 | #include "windows-nat.h" | |
9bb9e8ad | 68 | #include "i386-nat.h" |
ecc13e53 | 69 | #include "complaints.h" |
51a9c8c5 | 70 | #include "inf-child.h" |
de1b3c3d | 71 | |
418c6cb3 | 72 | #define AdjustTokenPrivileges dyn_AdjustTokenPrivileges |
2b008701 CF |
73 | #define DebugActiveProcessStop dyn_DebugActiveProcessStop |
74 | #define DebugBreakProcess dyn_DebugBreakProcess | |
75 | #define DebugSetProcessKillOnExit dyn_DebugSetProcessKillOnExit | |
76 | #define EnumProcessModules dyn_EnumProcessModules | |
2b008701 | 77 | #define GetModuleInformation dyn_GetModuleInformation |
418c6cb3 CF |
78 | #define LookupPrivilegeValueA dyn_LookupPrivilegeValueA |
79 | #define OpenProcessToken dyn_OpenProcessToken | |
cd44747c PM |
80 | #define GetConsoleFontSize dyn_GetConsoleFontSize |
81 | #define GetCurrentConsoleFont dyn_GetCurrentConsoleFont | |
2b008701 | 82 | |
418c6cb3 CF |
83 | static BOOL WINAPI (*AdjustTokenPrivileges)(HANDLE, BOOL, PTOKEN_PRIVILEGES, |
84 | DWORD, PTOKEN_PRIVILEGES, PDWORD); | |
2b008701 CF |
85 | static BOOL WINAPI (*DebugActiveProcessStop) (DWORD); |
86 | static BOOL WINAPI (*DebugBreakProcess) (HANDLE); | |
87 | static BOOL WINAPI (*DebugSetProcessKillOnExit) (BOOL); | |
88 | static BOOL WINAPI (*EnumProcessModules) (HANDLE, HMODULE *, DWORD, | |
89 | LPDWORD); | |
2b008701 CF |
90 | static BOOL WINAPI (*GetModuleInformation) (HANDLE, HMODULE, LPMODULEINFO, |
91 | DWORD); | |
418c6cb3 CF |
92 | static BOOL WINAPI (*LookupPrivilegeValueA)(LPCSTR, LPCSTR, PLUID); |
93 | static BOOL WINAPI (*OpenProcessToken)(HANDLE, DWORD, PHANDLE); | |
581e13c1 MS |
94 | static BOOL WINAPI (*GetCurrentConsoleFont) (HANDLE, BOOL, |
95 | CONSOLE_FONT_INFO *); | |
cd44747c | 96 | static COORD WINAPI (*GetConsoleFontSize) (HANDLE, DWORD); |
2b008701 | 97 | |
b3c613f2 CF |
98 | #undef STARTUPINFO |
99 | #undef CreateProcess | |
100 | #undef GetModuleFileNameEx | |
101 | ||
102 | #ifndef __CYGWIN__ | |
103 | # define __PMAX (MAX_PATH + 1) | |
104 | static DWORD WINAPI (*GetModuleFileNameEx) (HANDLE, HMODULE, LPSTR, DWORD); | |
105 | # define STARTUPINFO STARTUPINFOA | |
106 | # define CreateProcess CreateProcessA | |
107 | # define GetModuleFileNameEx_name "GetModuleFileNameExA" | |
108 | # define bad_GetModuleFileNameEx bad_GetModuleFileNameExA | |
109 | #else | |
110 | # define __PMAX PATH_MAX | |
581e13c1 | 111 | /* The starting and ending address of the cygwin1.dll text segment. */ |
b3c613f2 CF |
112 | static CORE_ADDR cygwin_load_start; |
113 | static CORE_ADDR cygwin_load_end; | |
b3c613f2 CF |
114 | # define __USEWIDE |
115 | typedef wchar_t cygwin_buf_t; | |
581e13c1 MS |
116 | static DWORD WINAPI (*GetModuleFileNameEx) (HANDLE, HMODULE, |
117 | LPWSTR, DWORD); | |
b3c613f2 CF |
118 | # define STARTUPINFO STARTUPINFOW |
119 | # define CreateProcess CreateProcessW | |
120 | # define GetModuleFileNameEx_name "GetModuleFileNameExW" | |
121 | # define bad_GetModuleFileNameEx bad_GetModuleFileNameExW | |
10325bc5 | 122 | #endif |
a244bdca | 123 | |
581e13c1 MS |
124 | static int have_saved_context; /* True if we've saved context from a |
125 | cygwin signal. */ | |
126 | static CONTEXT saved_context; /* Containes the saved context from a | |
127 | cygwin signal. */ | |
a244bdca | 128 | |
0714f9bf SS |
129 | /* If we're not using the old Cygwin header file set, define the |
130 | following which never should have been in the generic Win32 API | |
581e13c1 | 131 | headers in the first place since they were our own invention... */ |
0714f9bf | 132 | #ifndef _GNU_H_WINDOWS_H |
9d3789f7 | 133 | enum |
8e860359 CF |
134 | { |
135 | FLAG_TRACE_BIT = 0x100, | |
136 | CONTEXT_DEBUGGER = (CONTEXT_FULL | CONTEXT_FLOATING_POINT) | |
137 | }; | |
0714f9bf SS |
138 | #endif |
139 | ||
5851ab76 JB |
140 | #ifndef CONTEXT_EXTENDED_REGISTERS |
141 | /* This macro is only defined on ia32. It only makes sense on this target, | |
142 | so define it as zero if not already defined. */ | |
143 | #define CONTEXT_EXTENDED_REGISTERS 0 | |
144 | #endif | |
145 | ||
fa4ba8da PM |
146 | #define CONTEXT_DEBUGGER_DR CONTEXT_DEBUGGER | CONTEXT_DEBUG_REGISTERS \ |
147 | | CONTEXT_EXTENDED_REGISTERS | |
97da3b20 | 148 | |
41b4aadc | 149 | static uintptr_t dr[8]; |
87a45c96 CF |
150 | static int debug_registers_changed; |
151 | static int debug_registers_used; | |
16d905e2 CF |
152 | |
153 | static int windows_initialization_done; | |
6537bb24 | 154 | #define DR6_CLEAR_VALUE 0xffff0ff0 |
97da3b20 | 155 | |
3cee93ac | 156 | /* The string sent by cygwin when it processes a signal. |
581e13c1 | 157 | FIXME: This should be in a cygwin include file. */ |
3929abe9 CF |
158 | #ifndef _CYGWIN_SIGNAL_STRING |
159 | #define _CYGWIN_SIGNAL_STRING "cYgSiGw00f" | |
160 | #endif | |
3cee93ac | 161 | |
29fe111d | 162 | #define CHECK(x) check (x, __FILE__,__LINE__) |
dfe7f3ac | 163 | #define DEBUG_EXEC(x) if (debug_exec) printf_unfiltered x |
4e52d31c PM |
164 | #define DEBUG_EVENTS(x) if (debug_events) printf_unfiltered x |
165 | #define DEBUG_MEM(x) if (debug_memory) printf_unfiltered x | |
166 | #define DEBUG_EXCEPT(x) if (debug_exceptions) printf_unfiltered x | |
24e60978 | 167 | |
1eab8a48 | 168 | static void windows_stop (struct target_ops *self, ptid_t); |
02529b48 | 169 | static int windows_thread_alive (struct target_ops *, ptid_t); |
7d85a9c0 | 170 | static void windows_kill_inferior (struct target_ops *); |
3cee93ac | 171 | |
9bb9e8ad PM |
172 | static void cygwin_set_dr (int i, CORE_ADDR addr); |
173 | static void cygwin_set_dr7 (unsigned long val); | |
a961bc18 | 174 | static CORE_ADDR cygwin_get_dr (int i); |
9bb9e8ad | 175 | static unsigned long cygwin_get_dr6 (void); |
a961bc18 | 176 | static unsigned long cygwin_get_dr7 (void); |
9bb9e8ad | 177 | |
a493e3e2 | 178 | static enum gdb_signal last_sig = GDB_SIGNAL_0; |
581e13c1 | 179 | /* Set if a signal was received from the debugged process. */ |
7393af7c | 180 | |
3cee93ac | 181 | /* Thread information structure used to track information that is |
6537bb24 | 182 | not available in gdb's thread structure. */ |
3cee93ac | 183 | typedef struct thread_info_struct |
3a4b77d8 JM |
184 | { |
185 | struct thread_info_struct *next; | |
186 | DWORD id; | |
187 | HANDLE h; | |
711e434b | 188 | CORE_ADDR thread_local_base; |
3a4b77d8 | 189 | char *name; |
6537bb24 | 190 | int suspended; |
3ade5333 | 191 | int reload_context; |
3a4b77d8 | 192 | CONTEXT context; |
1e37c281 | 193 | STACKFRAME sf; |
8e860359 CF |
194 | } |
195 | thread_info; | |
1e37c281 | 196 | |
29fe111d | 197 | static thread_info thread_head; |
24e60978 | 198 | |
581e13c1 | 199 | /* The process and thread handles for the above context. */ |
24e60978 | 200 | |
3cee93ac CF |
201 | static DEBUG_EVENT current_event; /* The current debug event from |
202 | WaitForDebugEvent */ | |
203 | static HANDLE current_process_handle; /* Currently executing process */ | |
204 | static thread_info *current_thread; /* Info on currently selected thread */ | |
349b409f | 205 | static DWORD main_thread_id; /* Thread ID of the main thread */ |
24e60978 | 206 | |
581e13c1 | 207 | /* Counts of things. */ |
24e60978 SC |
208 | static int exception_count = 0; |
209 | static int event_count = 0; | |
dfe7f3ac | 210 | static int saw_create; |
bf25528d | 211 | static int open_process_used = 0; |
24e60978 | 212 | |
581e13c1 | 213 | /* User options. */ |
24e60978 | 214 | static int new_console = 0; |
10325bc5 | 215 | #ifdef __CYGWIN__ |
09280ddf | 216 | static int cygwin_exceptions = 0; |
10325bc5 | 217 | #endif |
1e37c281 | 218 | static int new_group = 1; |
dfe7f3ac CF |
219 | static int debug_exec = 0; /* show execution */ |
220 | static int debug_events = 0; /* show events from kernel */ | |
221 | static int debug_memory = 0; /* show target memory accesses */ | |
1ef980b9 | 222 | static int debug_exceptions = 0; /* show target exceptions */ |
dfe7f3ac CF |
223 | static int useshell = 0; /* use shell for subprocesses */ |
224 | ||
7e63b4e4 | 225 | /* This vector maps GDB's idea of a register's number into an offset |
dc05df57 | 226 | in the windows exception context vector. |
24e60978 | 227 | |
3cee93ac | 228 | It also contains the bit mask needed to load the register in question. |
24e60978 | 229 | |
7e63b4e4 JB |
230 | The contents of this table can only be computed by the units |
231 | that provide CPU-specific support for Windows native debugging. | |
232 | These units should set the table by calling | |
dc05df57 | 233 | windows_set_context_register_offsets. |
7e63b4e4 | 234 | |
24e60978 SC |
235 | One day we could read a reg, we could inspect the context we |
236 | already have loaded, if it doesn't have the bit set that we need, | |
237 | we read that set of registers in using GetThreadContext. If the | |
581e13c1 | 238 | context already contains what we need, we just unpack it. Then to |
24e60978 SC |
239 | write a register, first we have to ensure that the context contains |
240 | the other regs of the group, and then we copy the info in and set | |
581e13c1 | 241 | out bit. */ |
24e60978 | 242 | |
7e63b4e4 | 243 | static const int *mappings; |
d3a09475 | 244 | |
d40dc7a8 JB |
245 | /* The function to use in order to determine whether a register is |
246 | a segment register or not. */ | |
247 | static segment_register_p_ftype *segment_register_p; | |
248 | ||
24e60978 | 249 | /* This vector maps the target's idea of an exception (extracted |
581e13c1 | 250 | from the DEBUG_EVENT structure) to GDB's idea. */ |
24e60978 SC |
251 | |
252 | struct xlate_exception | |
253 | { | |
254 | int them; | |
2ea28649 | 255 | enum gdb_signal us; |
24e60978 SC |
256 | }; |
257 | ||
24e60978 SC |
258 | static const struct xlate_exception |
259 | xlate[] = | |
260 | { | |
a493e3e2 PA |
261 | {EXCEPTION_ACCESS_VIOLATION, GDB_SIGNAL_SEGV}, |
262 | {STATUS_STACK_OVERFLOW, GDB_SIGNAL_SEGV}, | |
263 | {EXCEPTION_BREAKPOINT, GDB_SIGNAL_TRAP}, | |
264 | {DBG_CONTROL_C, GDB_SIGNAL_INT}, | |
265 | {EXCEPTION_SINGLE_STEP, GDB_SIGNAL_TRAP}, | |
266 | {STATUS_FLOAT_DIVIDE_BY_ZERO, GDB_SIGNAL_FPE}, | |
24e60978 SC |
267 | {-1, -1}}; |
268 | ||
7e63b4e4 JB |
269 | /* Set the MAPPINGS static global to OFFSETS. |
270 | See the description of MAPPINGS for more details. */ | |
271 | ||
272 | void | |
dc05df57 | 273 | windows_set_context_register_offsets (const int *offsets) |
7e63b4e4 JB |
274 | { |
275 | mappings = offsets; | |
276 | } | |
277 | ||
d40dc7a8 JB |
278 | /* See windows-nat.h. */ |
279 | ||
280 | void | |
281 | windows_set_segment_register_p (segment_register_p_ftype *fun) | |
282 | { | |
283 | segment_register_p = fun; | |
284 | } | |
285 | ||
fa4ba8da PM |
286 | static void |
287 | check (BOOL ok, const char *file, int line) | |
288 | { | |
289 | if (!ok) | |
d50a0ce2 CV |
290 | printf_filtered ("error return %s:%d was %u\n", file, line, |
291 | (unsigned) GetLastError ()); | |
fa4ba8da PM |
292 | } |
293 | ||
6537bb24 PA |
294 | /* Find a thread record given a thread id. If GET_CONTEXT is not 0, |
295 | then also retrieve the context for this thread. If GET_CONTEXT is | |
296 | negative, then don't suspend the thread. */ | |
3cee93ac CF |
297 | static thread_info * |
298 | thread_rec (DWORD id, int get_context) | |
24e60978 | 299 | { |
3cee93ac CF |
300 | thread_info *th; |
301 | ||
3a4b77d8 | 302 | for (th = &thread_head; (th = th->next) != NULL;) |
3cee93ac CF |
303 | if (th->id == id) |
304 | { | |
6537bb24 | 305 | if (!th->suspended && get_context) |
3cee93ac | 306 | { |
8a892701 | 307 | if (get_context > 0 && id != current_event.dwThreadId) |
6537bb24 PA |
308 | { |
309 | if (SuspendThread (th->h) == (DWORD) -1) | |
310 | { | |
311 | DWORD err = GetLastError (); | |
22128028 | 312 | |
17617f2d EZ |
313 | /* We get Access Denied (5) when trying to suspend |
314 | threads that Windows started on behalf of the | |
315 | debuggee, usually when those threads are just | |
316 | about to exit. */ | |
317 | if (err != ERROR_ACCESS_DENIED) | |
318 | warning (_("SuspendThread (tid=0x%x) failed." | |
319 | " (winerr %u)"), | |
320 | (unsigned) id, (unsigned) err); | |
321 | th->suspended = -1; | |
6537bb24 | 322 | } |
17617f2d EZ |
323 | else |
324 | th->suspended = 1; | |
6537bb24 | 325 | } |
3cee93ac | 326 | else if (get_context < 0) |
6537bb24 | 327 | th->suspended = -1; |
3ade5333 | 328 | th->reload_context = 1; |
3cee93ac CF |
329 | } |
330 | return th; | |
331 | } | |
332 | ||
333 | return NULL; | |
334 | } | |
335 | ||
2dc38344 | 336 | /* Add a thread to the thread list. */ |
3cee93ac | 337 | static thread_info * |
711e434b | 338 | windows_add_thread (ptid_t ptid, HANDLE h, void *tlb) |
3cee93ac CF |
339 | { |
340 | thread_info *th; | |
2dc38344 PA |
341 | DWORD id; |
342 | ||
343 | gdb_assert (ptid_get_tid (ptid) != 0); | |
344 | ||
345 | id = ptid_get_tid (ptid); | |
3cee93ac CF |
346 | |
347 | if ((th = thread_rec (id, FALSE))) | |
348 | return th; | |
349 | ||
41bf6aca | 350 | th = XCNEW (thread_info); |
3cee93ac CF |
351 | th->id = id; |
352 | th->h = h; | |
711e434b | 353 | th->thread_local_base = (CORE_ADDR) (uintptr_t) tlb; |
3cee93ac CF |
354 | th->next = thread_head.next; |
355 | thread_head.next = th; | |
2dc38344 PA |
356 | add_thread (ptid); |
357 | /* Set the debug registers for the new thread if they are used. */ | |
fa4ba8da PM |
358 | if (debug_registers_used) |
359 | { | |
360 | /* Only change the value of the debug registers. */ | |
361 | th->context.ContextFlags = CONTEXT_DEBUG_REGISTERS; | |
362 | CHECK (GetThreadContext (th->h, &th->context)); | |
363 | th->context.Dr0 = dr[0]; | |
364 | th->context.Dr1 = dr[1]; | |
365 | th->context.Dr2 = dr[2]; | |
366 | th->context.Dr3 = dr[3]; | |
6537bb24 | 367 | th->context.Dr6 = DR6_CLEAR_VALUE; |
fa4ba8da PM |
368 | th->context.Dr7 = dr[7]; |
369 | CHECK (SetThreadContext (th->h, &th->context)); | |
370 | th->context.ContextFlags = 0; | |
371 | } | |
3cee93ac | 372 | return th; |
24e60978 SC |
373 | } |
374 | ||
3cee93ac | 375 | /* Clear out any old thread list and reintialize it to a |
581e13c1 | 376 | pristine state. */ |
24e60978 | 377 | static void |
dc05df57 | 378 | windows_init_thread_list (void) |
24e60978 | 379 | { |
3cee93ac CF |
380 | thread_info *th = &thread_head; |
381 | ||
dc05df57 | 382 | DEBUG_EVENTS (("gdb: windows_init_thread_list\n")); |
3cee93ac CF |
383 | init_thread_list (); |
384 | while (th->next != NULL) | |
24e60978 | 385 | { |
3cee93ac CF |
386 | thread_info *here = th->next; |
387 | th->next = here->next; | |
b8c9b27d | 388 | xfree (here); |
24e60978 | 389 | } |
059198c1 | 390 | thread_head.next = NULL; |
3cee93ac CF |
391 | } |
392 | ||
581e13c1 | 393 | /* Delete a thread from the list of threads. */ |
3cee93ac | 394 | static void |
e0ea48a0 | 395 | windows_delete_thread (ptid_t ptid, DWORD exit_code) |
3cee93ac CF |
396 | { |
397 | thread_info *th; | |
2dc38344 PA |
398 | DWORD id; |
399 | ||
400 | gdb_assert (ptid_get_tid (ptid) != 0); | |
401 | ||
402 | id = ptid_get_tid (ptid); | |
3cee93ac CF |
403 | |
404 | if (info_verbose) | |
2dc38344 | 405 | printf_unfiltered ("[Deleting %s]\n", target_pid_to_str (ptid)); |
e0ea48a0 EZ |
406 | else if (print_thread_events && id != main_thread_id) |
407 | printf_unfiltered (_("[%s exited with code %u]\n"), | |
564eac42 | 408 | target_pid_to_str (ptid), (unsigned) exit_code); |
2dc38344 | 409 | delete_thread (ptid); |
3cee93ac CF |
410 | |
411 | for (th = &thread_head; | |
412 | th->next != NULL && th->next->id != id; | |
413 | th = th->next) | |
414 | continue; | |
415 | ||
416 | if (th->next != NULL) | |
24e60978 | 417 | { |
3cee93ac CF |
418 | thread_info *here = th->next; |
419 | th->next = here->next; | |
b8c9b27d | 420 | xfree (here); |
24e60978 SC |
421 | } |
422 | } | |
423 | ||
3cee93ac | 424 | static void |
dc05df57 | 425 | do_windows_fetch_inferior_registers (struct regcache *regcache, int r) |
24e60978 | 426 | { |
1e37c281 | 427 | char *context_offset = ((char *) ¤t_thread->context) + mappings[r]; |
20a6ec49 MD |
428 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
429 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); | |
1e37c281 | 430 | long l; |
6c7de422 | 431 | |
3ade5333 | 432 | if (!current_thread) |
d6dc8049 | 433 | return; /* Windows sometimes uses a non-existent thread id in its |
581e13c1 | 434 | events. */ |
3ade5333 CF |
435 | |
436 | if (current_thread->reload_context) | |
437 | { | |
cb832706 | 438 | #ifdef __COPY_CONTEXT_SIZE |
a244bdca CF |
439 | if (have_saved_context) |
440 | { | |
581e13c1 MS |
441 | /* Lie about where the program actually is stopped since |
442 | cygwin has informed us that we should consider the signal | |
443 | to have occurred at another location which is stored in | |
444 | "saved_context. */ | |
445 | memcpy (¤t_thread->context, &saved_context, | |
446 | __COPY_CONTEXT_SIZE); | |
a244bdca CF |
447 | have_saved_context = 0; |
448 | } | |
449 | else | |
cb832706 | 450 | #endif |
a244bdca CF |
451 | { |
452 | thread_info *th = current_thread; | |
453 | th->context.ContextFlags = CONTEXT_DEBUGGER_DR; | |
17617f2d | 454 | CHECK (GetThreadContext (th->h, &th->context)); |
2b008701 | 455 | /* Copy dr values from that thread. |
581e13c1 MS |
456 | But only if there were not modified since last stop. |
457 | PR gdb/2388 */ | |
88616312 PM |
458 | if (!debug_registers_changed) |
459 | { | |
460 | dr[0] = th->context.Dr0; | |
461 | dr[1] = th->context.Dr1; | |
462 | dr[2] = th->context.Dr2; | |
463 | dr[3] = th->context.Dr3; | |
464 | dr[6] = th->context.Dr6; | |
465 | dr[7] = th->context.Dr7; | |
466 | } | |
a244bdca | 467 | } |
3ade5333 CF |
468 | current_thread->reload_context = 0; |
469 | } | |
470 | ||
20a6ec49 | 471 | if (r == I387_FISEG_REGNUM (tdep)) |
1e37c281 | 472 | { |
8e860359 | 473 | l = *((long *) context_offset) & 0xffff; |
56be3814 | 474 | regcache_raw_supply (regcache, r, (char *) &l); |
1e37c281 | 475 | } |
20a6ec49 | 476 | else if (r == I387_FOP_REGNUM (tdep)) |
1e37c281 | 477 | { |
8e860359 | 478 | l = (*((long *) context_offset) >> 16) & ((1 << 11) - 1); |
56be3814 | 479 | regcache_raw_supply (regcache, r, (char *) &l); |
1e37c281 | 480 | } |
d40dc7a8 JB |
481 | else if (segment_register_p (r)) |
482 | { | |
483 | /* GDB treats segment registers as 32bit registers, but they are | |
484 | in fact only 16 bits long. Make sure we do not read extra | |
485 | bits from our source buffer. */ | |
486 | l = *((long *) context_offset) & 0xffff; | |
487 | regcache_raw_supply (regcache, r, (char *) &l); | |
488 | } | |
1e37c281 | 489 | else if (r >= 0) |
56be3814 | 490 | regcache_raw_supply (regcache, r, context_offset); |
3cee93ac | 491 | else |
24e60978 | 492 | { |
20a6ec49 | 493 | for (r = 0; r < gdbarch_num_regs (gdbarch); r++) |
dc05df57 | 494 | do_windows_fetch_inferior_registers (regcache, r); |
24e60978 | 495 | } |
3cee93ac CF |
496 | } |
497 | ||
498 | static void | |
28439f5e PA |
499 | windows_fetch_inferior_registers (struct target_ops *ops, |
500 | struct regcache *regcache, int r) | |
3cee93ac | 501 | { |
2dc38344 | 502 | current_thread = thread_rec (ptid_get_tid (inferior_ptid), TRUE); |
d6dc8049 | 503 | /* Check if current_thread exists. Windows sometimes uses a non-existent |
581e13c1 | 504 | thread id in its events. */ |
3ade5333 | 505 | if (current_thread) |
dc05df57 | 506 | do_windows_fetch_inferior_registers (regcache, r); |
3cee93ac CF |
507 | } |
508 | ||
509 | static void | |
dc05df57 | 510 | do_windows_store_inferior_registers (const struct regcache *regcache, int r) |
3cee93ac | 511 | { |
3ade5333 | 512 | if (!current_thread) |
581e13c1 | 513 | /* Windows sometimes uses a non-existent thread id in its events. */; |
3ade5333 | 514 | else if (r >= 0) |
56be3814 | 515 | regcache_raw_collect (regcache, r, |
822c9732 | 516 | ((char *) ¤t_thread->context) + mappings[r]); |
24e60978 SC |
517 | else |
518 | { | |
40a6adc1 | 519 | for (r = 0; r < gdbarch_num_regs (get_regcache_arch (regcache)); r++) |
dc05df57 | 520 | do_windows_store_inferior_registers (regcache, r); |
24e60978 SC |
521 | } |
522 | } | |
523 | ||
581e13c1 | 524 | /* Store a new register value into the current thread context. */ |
3cee93ac | 525 | static void |
28439f5e PA |
526 | windows_store_inferior_registers (struct target_ops *ops, |
527 | struct regcache *regcache, int r) | |
3cee93ac | 528 | { |
2dc38344 | 529 | current_thread = thread_rec (ptid_get_tid (inferior_ptid), TRUE); |
d6dc8049 | 530 | /* Check if current_thread exists. Windows sometimes uses a non-existent |
581e13c1 | 531 | thread id in its events. */ |
3ade5333 | 532 | if (current_thread) |
dc05df57 | 533 | do_windows_store_inferior_registers (regcache, r); |
3cee93ac | 534 | } |
24e60978 | 535 | |
450005e7 | 536 | /* Encapsulate the information required in a call to |
581e13c1 | 537 | symbol_file_add_args. */ |
8a892701 CF |
538 | struct safe_symbol_file_add_args |
539 | { | |
540 | char *name; | |
541 | int from_tty; | |
542 | struct section_addr_info *addrs; | |
543 | int mainline; | |
544 | int flags; | |
7c5c87c0 | 545 | struct ui_file *err, *out; |
8a892701 CF |
546 | struct objfile *ret; |
547 | }; | |
548 | ||
581e13c1 | 549 | /* Maintain a linked list of "so" information. */ |
3ee6f623 | 550 | struct lm_info |
02e423b9 | 551 | { |
d3653bf6 | 552 | LPVOID load_addr; |
3ee6f623 CF |
553 | }; |
554 | ||
555 | static struct so_list solib_start, *solib_end; | |
02e423b9 | 556 | |
450005e7 | 557 | /* Call symbol_file_add with stderr redirected. We don't care if there |
581e13c1 | 558 | are errors. */ |
8a892701 CF |
559 | static int |
560 | safe_symbol_file_add_stub (void *argv) | |
561 | { | |
3ee6f623 | 562 | #define p ((struct safe_symbol_file_add_args *) argv) |
7eedccfa PP |
563 | const int add_flags = ((p->from_tty ? SYMFILE_VERBOSE : 0) |
564 | | (p->mainline ? SYMFILE_MAINLINE : 0)); | |
565 | p->ret = symbol_file_add (p->name, add_flags, p->addrs, p->flags); | |
8a892701 CF |
566 | return !!p->ret; |
567 | #undef p | |
568 | } | |
569 | ||
581e13c1 | 570 | /* Restore gdb's stderr after calling symbol_file_add. */ |
8a892701 | 571 | static void |
7c5c87c0 | 572 | safe_symbol_file_add_cleanup (void *p) |
8a892701 | 573 | { |
8e860359 | 574 | #define sp ((struct safe_symbol_file_add_args *)p) |
450005e7 | 575 | gdb_flush (gdb_stderr); |
7c5c87c0 | 576 | gdb_flush (gdb_stdout); |
d3ff4a77 | 577 | ui_file_delete (gdb_stderr); |
7c5c87c0 | 578 | ui_file_delete (gdb_stdout); |
d3ff4a77 | 579 | gdb_stderr = sp->err; |
9d3789f7 | 580 | gdb_stdout = sp->out; |
8e860359 | 581 | #undef sp |
8a892701 CF |
582 | } |
583 | ||
581e13c1 | 584 | /* symbol_file_add wrapper that prevents errors from being displayed. */ |
8a892701 CF |
585 | static struct objfile * |
586 | safe_symbol_file_add (char *name, int from_tty, | |
587 | struct section_addr_info *addrs, | |
588 | int mainline, int flags) | |
8a892701 CF |
589 | { |
590 | struct safe_symbol_file_add_args p; | |
591 | struct cleanup *cleanup; | |
592 | ||
7c5c87c0 | 593 | cleanup = make_cleanup (safe_symbol_file_add_cleanup, &p); |
8a892701 | 594 | |
7c5c87c0 CF |
595 | p.err = gdb_stderr; |
596 | p.out = gdb_stdout; | |
450005e7 | 597 | gdb_flush (gdb_stderr); |
7c5c87c0 | 598 | gdb_flush (gdb_stdout); |
d3ff4a77 | 599 | gdb_stderr = ui_file_new (); |
7c5c87c0 | 600 | gdb_stdout = ui_file_new (); |
8a892701 CF |
601 | p.name = name; |
602 | p.from_tty = from_tty; | |
603 | p.addrs = addrs; | |
604 | p.mainline = mainline; | |
605 | p.flags = flags; | |
606 | catch_errors (safe_symbol_file_add_stub, &p, "", RETURN_MASK_ERROR); | |
607 | ||
608 | do_cleanups (cleanup); | |
609 | return p.ret; | |
610 | } | |
611 | ||
de1b3c3d | 612 | static struct so_list * |
dc05df57 | 613 | windows_make_so (const char *name, LPVOID load_addr) |
8e860359 | 614 | { |
3ee6f623 | 615 | struct so_list *so; |
d0d0ab16 CV |
616 | char *p; |
617 | #ifndef __CYGWIN__ | |
b3c613f2 CF |
618 | char buf[__PMAX]; |
619 | char cwd[__PMAX]; | |
3f8ad85b CF |
620 | WIN32_FIND_DATA w32_fd; |
621 | HANDLE h = FindFirstFile(name, &w32_fd); | |
3f8ad85b | 622 | |
6badb179 CF |
623 | if (h == INVALID_HANDLE_VALUE) |
624 | strcpy (buf, name); | |
625 | else | |
3f8ad85b | 626 | { |
c914e0cc CF |
627 | FindClose (h); |
628 | strcpy (buf, name); | |
629 | if (GetCurrentDirectory (MAX_PATH + 1, cwd)) | |
630 | { | |
631 | p = strrchr (buf, '\\'); | |
632 | if (p) | |
633 | p[1] = '\0'; | |
634 | SetCurrentDirectory (buf); | |
635 | GetFullPathName (w32_fd.cFileName, MAX_PATH, buf, &p); | |
636 | SetCurrentDirectory (cwd); | |
637 | } | |
3f8ad85b | 638 | } |
3ee6f623 CF |
639 | if (strcasecmp (buf, "ntdll.dll") == 0) |
640 | { | |
641 | GetSystemDirectory (buf, sizeof (buf)); | |
642 | strcat (buf, "\\ntdll.dll"); | |
643 | } | |
d0d0ab16 | 644 | #else |
b3c613f2 | 645 | cygwin_buf_t buf[__PMAX]; |
d0d0ab16 | 646 | |
b3c613f2 | 647 | buf[0] = 0; |
d0d0ab16 CV |
648 | if (access (name, F_OK) != 0) |
649 | { | |
650 | if (strcasecmp (name, "ntdll.dll") == 0) | |
b3c613f2 | 651 | #ifdef __USEWIDE |
d0d0ab16 CV |
652 | { |
653 | GetSystemDirectoryW (buf, sizeof (buf) / sizeof (wchar_t)); | |
654 | wcscat (buf, L"\\ntdll.dll"); | |
655 | } | |
b3c613f2 CF |
656 | #else |
657 | { | |
658 | GetSystemDirectoryA (buf, sizeof (buf) / sizeof (wchar_t)); | |
659 | strcat (buf, "\\ntdll.dll"); | |
660 | } | |
661 | #endif | |
d0d0ab16 CV |
662 | } |
663 | #endif | |
41bf6aca | 664 | so = XCNEW (struct so_list); |
3ee6f623 CF |
665 | so->lm_info = (struct lm_info *) xmalloc (sizeof (struct lm_info)); |
666 | so->lm_info->load_addr = load_addr; | |
de1b3c3d | 667 | strcpy (so->so_original_name, name); |
10325bc5 PA |
668 | #ifndef __CYGWIN__ |
669 | strcpy (so->so_name, buf); | |
670 | #else | |
d0d0ab16 CV |
671 | if (buf[0]) |
672 | cygwin_conv_path (CCP_WIN_W_TO_POSIX, buf, so->so_name, | |
673 | SO_NAME_MAX_PATH_SIZE); | |
674 | else | |
675 | { | |
60c5c021 | 676 | char *rname = realpath (name, NULL); |
d0d0ab16 CV |
677 | if (rname && strlen (rname) < SO_NAME_MAX_PATH_SIZE) |
678 | { | |
679 | strcpy (so->so_name, rname); | |
680 | free (rname); | |
681 | } | |
682 | else | |
683 | error (_("dll path too long")); | |
684 | } | |
de1b3c3d PA |
685 | /* Record cygwin1.dll .text start/end. */ |
686 | p = strchr (so->so_name, '\0') - (sizeof ("/cygwin1.dll") - 1); | |
687 | if (p >= so->so_name && strcasecmp (p, "/cygwin1.dll") == 0) | |
688 | { | |
689 | bfd *abfd; | |
690 | asection *text = NULL; | |
691 | CORE_ADDR text_vma; | |
8e860359 | 692 | |
1c00ec6b | 693 | abfd = gdb_bfd_open (so->so_name, "pei-i386", -1); |
a244bdca | 694 | |
de1b3c3d PA |
695 | if (!abfd) |
696 | return so; | |
697 | ||
698 | if (bfd_check_format (abfd, bfd_object)) | |
699 | text = bfd_get_section_by_name (abfd, ".text"); | |
700 | ||
701 | if (!text) | |
702 | { | |
cbb099e8 | 703 | gdb_bfd_unref (abfd); |
de1b3c3d PA |
704 | return so; |
705 | } | |
706 | ||
7a9dd1b2 | 707 | /* The symbols in a dll are offset by 0x1000, which is the |
de1b3c3d | 708 | offset from 0 of the first byte in an image - because of the |
581e13c1 MS |
709 | file header and the section alignment. */ |
710 | cygwin_load_start = (CORE_ADDR) (uintptr_t) ((char *) | |
711 | load_addr + 0x1000); | |
dc05df57 | 712 | cygwin_load_end = cygwin_load_start + bfd_section_size (abfd, text); |
de1b3c3d | 713 | |
cbb099e8 | 714 | gdb_bfd_unref (abfd); |
de1b3c3d | 715 | } |
10325bc5 | 716 | #endif |
de1b3c3d PA |
717 | |
718 | return so; | |
8e860359 CF |
719 | } |
720 | ||
3ee6f623 | 721 | static char * |
dfe7f3ac CF |
722 | get_image_name (HANDLE h, void *address, int unicode) |
723 | { | |
d0d0ab16 | 724 | #ifdef __CYGWIN__ |
b3c613f2 | 725 | static char buf[__PMAX]; |
d0d0ab16 | 726 | #else |
b3c613f2 | 727 | static char buf[(2 * __PMAX) + 1]; |
d0d0ab16 | 728 | #endif |
dfe7f3ac CF |
729 | DWORD size = unicode ? sizeof (WCHAR) : sizeof (char); |
730 | char *address_ptr; | |
731 | int len = 0; | |
732 | char b[2]; | |
5732a500 | 733 | SIZE_T done; |
dfe7f3ac CF |
734 | |
735 | /* Attempt to read the name of the dll that was detected. | |
736 | This is documented to work only when actively debugging | |
581e13c1 | 737 | a program. It will not work for attached processes. */ |
dfe7f3ac CF |
738 | if (address == NULL) |
739 | return NULL; | |
740 | ||
dfe7f3ac | 741 | /* See if we could read the address of a string, and that the |
581e13c1 MS |
742 | address isn't null. */ |
743 | if (!ReadProcessMemory (h, address, &address_ptr, | |
744 | sizeof (address_ptr), &done) | |
6f17862b | 745 | || done != sizeof (address_ptr) || !address_ptr) |
dfe7f3ac CF |
746 | return NULL; |
747 | ||
581e13c1 | 748 | /* Find the length of the string. */ |
6f17862b CF |
749 | while (ReadProcessMemory (h, address_ptr + len++ * size, &b, size, &done) |
750 | && (b[0] != 0 || b[size - 1] != 0) && done == size) | |
751 | continue; | |
dfe7f3ac CF |
752 | |
753 | if (!unicode) | |
754 | ReadProcessMemory (h, address_ptr, buf, len, &done); | |
755 | else | |
756 | { | |
757 | WCHAR *unicode_address = (WCHAR *) alloca (len * sizeof (WCHAR)); | |
758 | ReadProcessMemory (h, address_ptr, unicode_address, len * sizeof (WCHAR), | |
759 | &done); | |
d0d0ab16 | 760 | #ifdef __CYGWIN__ |
b3c613f2 | 761 | wcstombs (buf, unicode_address, __PMAX); |
d0d0ab16 CV |
762 | #else |
763 | WideCharToMultiByte (CP_ACP, 0, unicode_address, len, buf, sizeof buf, | |
764 | 0, 0); | |
765 | #endif | |
dfe7f3ac CF |
766 | } |
767 | ||
768 | return buf; | |
769 | } | |
770 | ||
1cd9feab JB |
771 | /* Handle a DLL load event, and return 1. |
772 | ||
773 | This function assumes that this event did not occur during inferior | |
774 | initialization, where their event info may be incomplete (see | |
775 | do_initial_windows_stuff and windows_add_all_dlls for more info | |
776 | on how we handle DLL loading during that phase). */ | |
777 | ||
1750a5ef | 778 | static int |
0a65a603 | 779 | handle_load_dll (void *dummy) |
24e60978 | 780 | { |
3a4b77d8 | 781 | LOAD_DLL_DEBUG_INFO *event = ¤t_event.u.LoadDll; |
1cd9feab | 782 | char *dll_name; |
24e60978 | 783 | |
94481b8c JB |
784 | /* Try getting the DLL name via the lpImageName field of the event. |
785 | Note that Microsoft documents this fields as strictly optional, | |
786 | in the sense that it might be NULL. And the first DLL event in | |
787 | particular is explicitly documented as "likely not pass[ed]" | |
788 | (source: MSDN LOAD_DLL_DEBUG_INFO structure). */ | |
1cd9feab JB |
789 | dll_name = get_image_name (current_process_handle, |
790 | event->lpImageName, event->fUnicode); | |
3cee93ac CF |
791 | if (!dll_name) |
792 | return 1; | |
793 | ||
dc05df57 | 794 | solib_end->next = windows_make_so (dll_name, event->lpBaseOfDll); |
de1b3c3d | 795 | solib_end = solib_end->next; |
450005e7 | 796 | |
a74ce742 PM |
797 | DEBUG_EVENTS (("gdb: Loading dll \"%s\" at %s.\n", solib_end->so_name, |
798 | host_address_to_string (solib_end->lm_info->load_addr))); | |
7488902c | 799 | |
450005e7 CF |
800 | return 1; |
801 | } | |
802 | ||
3ee6f623 | 803 | static void |
dc05df57 | 804 | windows_free_so (struct so_list *so) |
3ee6f623 | 805 | { |
3ee6f623 CF |
806 | if (so->lm_info) |
807 | xfree (so->lm_info); | |
de1b3c3d | 808 | xfree (so); |
3cb8e7f6 CF |
809 | } |
810 | ||
3be75f87 JB |
811 | /* Handle a DLL unload event. |
812 | Return 1 if successful, or zero otherwise. | |
813 | ||
814 | This function assumes that this event did not occur during inferior | |
815 | initialization, where their event info may be incomplete (see | |
816 | do_initial_windows_stuff and windows_add_all_dlls for more info | |
817 | on how we handle DLL loading during that phase). */ | |
818 | ||
d3ff4a77 | 819 | static int |
0a65a603 | 820 | handle_unload_dll (void *dummy) |
d3ff4a77 | 821 | { |
d3653bf6 | 822 | LPVOID lpBaseOfDll = current_event.u.UnloadDll.lpBaseOfDll; |
3ee6f623 | 823 | struct so_list *so; |
d3ff4a77 CF |
824 | |
825 | for (so = &solib_start; so->next != NULL; so = so->next) | |
3ee6f623 | 826 | if (so->next->lm_info->load_addr == lpBaseOfDll) |
d3ff4a77 | 827 | { |
3ee6f623 | 828 | struct so_list *sodel = so->next; |
a25cd31f | 829 | |
d3ff4a77 CF |
830 | so->next = sodel->next; |
831 | if (!so->next) | |
832 | solib_end = so; | |
7488902c PM |
833 | DEBUG_EVENTS (("gdb: Unloading dll \"%s\".\n", sodel->so_name)); |
834 | ||
dc05df57 | 835 | windows_free_so (sodel); |
d3ff4a77 CF |
836 | return 1; |
837 | } | |
3929abe9 | 838 | |
ecc13e53 JB |
839 | /* We did not find any DLL that was previously loaded at this address, |
840 | so register a complaint. We do not report an error, because we have | |
841 | observed that this may be happening under some circumstances. For | |
842 | instance, running 32bit applications on x64 Windows causes us to receive | |
843 | 4 mysterious UNLOAD_DLL_DEBUG_EVENTs during the startup phase (these | |
844 | events are apparently caused by the WOW layer, the interface between | |
845 | 32bit and 64bit worlds). */ | |
846 | complaint (&symfile_complaints, _("dll starting at %s not found."), | |
847 | host_address_to_string (lpBaseOfDll)); | |
d3ff4a77 CF |
848 | |
849 | return 0; | |
850 | } | |
851 | ||
581e13c1 | 852 | /* Clear list of loaded DLLs. */ |
3ee6f623 | 853 | static void |
dc05df57 | 854 | windows_clear_solib (void) |
450005e7 | 855 | { |
450005e7 CF |
856 | solib_start.next = NULL; |
857 | solib_end = &solib_start; | |
450005e7 | 858 | } |
295732ea | 859 | |
581e13c1 | 860 | /* Load DLL symbol info. */ |
d603d4b3 | 861 | static void |
7470a420 | 862 | dll_symbol_command (char *args, int from_tty) |
450005e7 | 863 | { |
8e860359 | 864 | int n; |
450005e7 | 865 | dont_repeat (); |
8e860359 | 866 | |
450005e7 | 867 | if (args == NULL) |
8a3fe4f8 | 868 | error (_("dll-symbols requires a file name")); |
450005e7 | 869 | |
8e860359 CF |
870 | n = strlen (args); |
871 | if (n > 4 && strcasecmp (args + n - 4, ".dll") != 0) | |
872 | { | |
873 | char *newargs = (char *) alloca (n + 4 + 1); | |
874 | strcpy (newargs, args); | |
875 | strcat (newargs, ".dll"); | |
876 | args = newargs; | |
877 | } | |
878 | ||
7470a420 | 879 | safe_symbol_file_add (args, from_tty, NULL, 0, OBJF_SHARED | OBJF_USERLOADED); |
8e860359 | 880 | } |
450005e7 | 881 | |
3cee93ac CF |
882 | /* Handle DEBUG_STRING output from child process. |
883 | Cygwin prepends its messages with a "cygwin:". Interpret this as | |
581e13c1 | 884 | a Cygwin signal. Otherwise just print the string as a warning. */ |
3cee93ac CF |
885 | static int |
886 | handle_output_debug_string (struct target_waitstatus *ourstatus) | |
887 | { | |
a244bdca CF |
888 | char *s = NULL; |
889 | int retval = 0; | |
3cee93ac CF |
890 | |
891 | if (!target_read_string | |
2c647436 PM |
892 | ((CORE_ADDR) (uintptr_t) current_event.u.DebugString.lpDebugStringData, |
893 | &s, 1024, 0) | |
3cee93ac | 894 | || !s || !*s) |
a244bdca | 895 | /* nothing to do */; |
581e13c1 MS |
896 | else if (strncmp (s, _CYGWIN_SIGNAL_STRING, |
897 | sizeof (_CYGWIN_SIGNAL_STRING) - 1) != 0) | |
3cee93ac | 898 | { |
10325bc5 | 899 | #ifdef __CYGWIN__ |
d3a09475 | 900 | if (strncmp (s, "cYg", 3) != 0) |
10325bc5 | 901 | #endif |
8a3fe4f8 | 902 | warning (("%s"), s); |
3cee93ac | 903 | } |
cb832706 | 904 | #ifdef __COPY_CONTEXT_SIZE |
d3a09475 | 905 | else |
3cee93ac | 906 | { |
581e13c1 MS |
907 | /* Got a cygwin signal marker. A cygwin signal is followed by |
908 | the signal number itself and then optionally followed by the | |
909 | thread id and address to saved context within the DLL. If | |
910 | these are supplied, then the given thread is assumed to have | |
911 | issued the signal and the context from the thread is assumed | |
912 | to be stored at the given address in the inferior. Tell gdb | |
913 | to treat this like a real signal. */ | |
3cee93ac | 914 | char *p; |
3929abe9 | 915 | int sig = strtol (s + sizeof (_CYGWIN_SIGNAL_STRING) - 1, &p, 0); |
2ea28649 | 916 | int gotasig = gdb_signal_from_host (sig); |
c62fa0e2 | 917 | |
0714f9bf SS |
918 | ourstatus->value.sig = gotasig; |
919 | if (gotasig) | |
a244bdca CF |
920 | { |
921 | LPCVOID x; | |
2c15ef43 | 922 | SIZE_T n; |
c62fa0e2 | 923 | |
a244bdca CF |
924 | ourstatus->kind = TARGET_WAITKIND_STOPPED; |
925 | retval = strtoul (p, &p, 0); | |
926 | if (!retval) | |
927 | retval = main_thread_id; | |
40653b35 | 928 | else if ((x = (LPCVOID) (uintptr_t) strtoull (p, NULL, 0)) |
a244bdca | 929 | && ReadProcessMemory (current_process_handle, x, |
581e13c1 MS |
930 | &saved_context, |
931 | __COPY_CONTEXT_SIZE, &n) | |
a244bdca CF |
932 | && n == __COPY_CONTEXT_SIZE) |
933 | have_saved_context = 1; | |
934 | current_event.dwThreadId = retval; | |
935 | } | |
3cee93ac | 936 | } |
cb832706 | 937 | #endif |
3cee93ac | 938 | |
a244bdca CF |
939 | if (s) |
940 | xfree (s); | |
941 | return retval; | |
3cee93ac | 942 | } |
24e60978 | 943 | |
c1748f97 PM |
944 | static int |
945 | display_selector (HANDLE thread, DWORD sel) | |
946 | { | |
947 | LDT_ENTRY info; | |
948 | if (GetThreadSelectorEntry (thread, sel, &info)) | |
949 | { | |
950 | int base, limit; | |
d50a0ce2 | 951 | printf_filtered ("0x%03x: ", (unsigned) sel); |
c1748f97 | 952 | if (!info.HighWord.Bits.Pres) |
baa93fa6 CF |
953 | { |
954 | puts_filtered ("Segment not present\n"); | |
955 | return 0; | |
956 | } | |
c1748f97 PM |
957 | base = (info.HighWord.Bits.BaseHi << 24) + |
958 | (info.HighWord.Bits.BaseMid << 16) | |
959 | + info.BaseLow; | |
960 | limit = (info.HighWord.Bits.LimitHi << 16) + info.LimitLow; | |
961 | if (info.HighWord.Bits.Granularity) | |
caad7706 | 962 | limit = (limit << 12) | 0xfff; |
c1748f97 PM |
963 | printf_filtered ("base=0x%08x limit=0x%08x", base, limit); |
964 | if (info.HighWord.Bits.Default_Big) | |
baa93fa6 | 965 | puts_filtered(" 32-bit "); |
c1748f97 | 966 | else |
baa93fa6 | 967 | puts_filtered(" 16-bit "); |
c1748f97 PM |
968 | switch ((info.HighWord.Bits.Type & 0xf) >> 1) |
969 | { | |
970 | case 0: | |
baa93fa6 CF |
971 | puts_filtered ("Data (Read-Only, Exp-up"); |
972 | break; | |
c1748f97 | 973 | case 1: |
baa93fa6 CF |
974 | puts_filtered ("Data (Read/Write, Exp-up"); |
975 | break; | |
c1748f97 | 976 | case 2: |
baa93fa6 CF |
977 | puts_filtered ("Unused segment ("); |
978 | break; | |
c1748f97 | 979 | case 3: |
baa93fa6 CF |
980 | puts_filtered ("Data (Read/Write, Exp-down"); |
981 | break; | |
c1748f97 | 982 | case 4: |
baa93fa6 CF |
983 | puts_filtered ("Code (Exec-Only, N.Conf"); |
984 | break; | |
c1748f97 | 985 | case 5: |
baa93fa6 | 986 | puts_filtered ("Code (Exec/Read, N.Conf"); |
c1748f97 PM |
987 | break; |
988 | case 6: | |
baa93fa6 | 989 | puts_filtered ("Code (Exec-Only, Conf"); |
c1748f97 PM |
990 | break; |
991 | case 7: | |
baa93fa6 | 992 | puts_filtered ("Code (Exec/Read, Conf"); |
c1748f97 PM |
993 | break; |
994 | default: | |
995 | printf_filtered ("Unknown type 0x%x",info.HighWord.Bits.Type); | |
996 | } | |
997 | if ((info.HighWord.Bits.Type & 0x1) == 0) | |
baa93fa6 | 998 | puts_filtered(", N.Acc"); |
c1748f97 PM |
999 | puts_filtered (")\n"); |
1000 | if ((info.HighWord.Bits.Type & 0x10) == 0) | |
1001 | puts_filtered("System selector "); | |
1002 | printf_filtered ("Priviledge level = %d. ", info.HighWord.Bits.Dpl); | |
1003 | if (info.HighWord.Bits.Granularity) | |
baa93fa6 | 1004 | puts_filtered ("Page granular.\n"); |
c1748f97 PM |
1005 | else |
1006 | puts_filtered ("Byte granular.\n"); | |
1007 | return 1; | |
1008 | } | |
1009 | else | |
1010 | { | |
5572ce1f PM |
1011 | DWORD err = GetLastError (); |
1012 | if (err == ERROR_NOT_SUPPORTED) | |
1013 | printf_filtered ("Function not supported\n"); | |
1014 | else | |
d50a0ce2 | 1015 | printf_filtered ("Invalid selector 0x%x.\n", (unsigned) sel); |
c1748f97 PM |
1016 | return 0; |
1017 | } | |
1018 | } | |
1019 | ||
1020 | static void | |
1021 | display_selectors (char * args, int from_tty) | |
1022 | { | |
1023 | if (!current_thread) | |
1024 | { | |
1025 | puts_filtered ("Impossible to display selectors now.\n"); | |
1026 | return; | |
1027 | } | |
1028 | if (!args) | |
1029 | { | |
1030 | ||
1031 | puts_filtered ("Selector $cs\n"); | |
1032 | display_selector (current_thread->h, | |
baa93fa6 | 1033 | current_thread->context.SegCs); |
c1748f97 PM |
1034 | puts_filtered ("Selector $ds\n"); |
1035 | display_selector (current_thread->h, | |
baa93fa6 | 1036 | current_thread->context.SegDs); |
c1748f97 PM |
1037 | puts_filtered ("Selector $es\n"); |
1038 | display_selector (current_thread->h, | |
baa93fa6 | 1039 | current_thread->context.SegEs); |
c1748f97 PM |
1040 | puts_filtered ("Selector $ss\n"); |
1041 | display_selector (current_thread->h, | |
baa93fa6 | 1042 | current_thread->context.SegSs); |
c1748f97 PM |
1043 | puts_filtered ("Selector $fs\n"); |
1044 | display_selector (current_thread->h, | |
1045 | current_thread->context.SegFs); | |
1046 | puts_filtered ("Selector $gs\n"); | |
1047 | display_selector (current_thread->h, | |
baa93fa6 | 1048 | current_thread->context.SegGs); |
c1748f97 PM |
1049 | } |
1050 | else | |
1051 | { | |
1052 | int sel; | |
1053 | sel = parse_and_eval_long (args); | |
1054 | printf_filtered ("Selector \"%s\"\n",args); | |
1055 | display_selector (current_thread->h, sel); | |
1056 | } | |
1057 | } | |
1058 | ||
7393af7c | 1059 | #define DEBUG_EXCEPTION_SIMPLE(x) if (debug_exceptions) \ |
a74ce742 PM |
1060 | printf_unfiltered ("gdb: Target exception %s at %s\n", x, \ |
1061 | host_address_to_string (\ | |
1062 | current_event.u.Exception.ExceptionRecord.ExceptionAddress)) | |
7393af7c | 1063 | |
36339ecd | 1064 | static int |
450005e7 | 1065 | handle_exception (struct target_waitstatus *ourstatus) |
24e60978 | 1066 | { |
3cee93ac | 1067 | thread_info *th; |
29fe111d | 1068 | DWORD code = current_event.u.Exception.ExceptionRecord.ExceptionCode; |
3cee93ac | 1069 | |
29fe111d | 1070 | ourstatus->kind = TARGET_WAITKIND_STOPPED; |
8a892701 | 1071 | |
581e13c1 | 1072 | /* Record the context of the current thread. */ |
3cee93ac | 1073 | th = thread_rec (current_event.dwThreadId, -1); |
24e60978 | 1074 | |
29fe111d | 1075 | switch (code) |
24e60978 | 1076 | { |
1ef980b9 | 1077 | case EXCEPTION_ACCESS_VIOLATION: |
7393af7c | 1078 | DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_ACCESS_VIOLATION"); |
a493e3e2 | 1079 | ourstatus->value.sig = GDB_SIGNAL_SEGV; |
10325bc5 | 1080 | #ifdef __CYGWIN__ |
8da8e0b3 | 1081 | { |
581e13c1 MS |
1082 | /* See if the access violation happened within the cygwin DLL |
1083 | itself. Cygwin uses a kind of exception handling to deal | |
1084 | with passed-in invalid addresses. gdb should not treat | |
1085 | these as real SEGVs since they will be silently handled by | |
1086 | cygwin. A real SEGV will (theoretically) be caught by | |
1087 | cygwin later in the process and will be sent as a | |
1088 | cygwin-specific-signal. So, ignore SEGVs if they show up | |
1089 | within the text segment of the DLL itself. */ | |
2c02bd72 | 1090 | const char *fn; |
581e13c1 MS |
1091 | CORE_ADDR addr = (CORE_ADDR) (uintptr_t) |
1092 | current_event.u.Exception.ExceptionRecord.ExceptionAddress; | |
1093 | ||
1094 | if ((!cygwin_exceptions && (addr >= cygwin_load_start | |
1095 | && addr < cygwin_load_end)) | |
a244bdca | 1096 | || (find_pc_partial_function (addr, &fn, NULL, NULL) |
581e13c1 MS |
1097 | && strncmp (fn, "KERNEL32!IsBad", |
1098 | strlen ("KERNEL32!IsBad")) == 0)) | |
8da8e0b3 CF |
1099 | return 0; |
1100 | } | |
10325bc5 | 1101 | #endif |
7393af7c PM |
1102 | break; |
1103 | case STATUS_STACK_OVERFLOW: | |
1104 | DEBUG_EXCEPTION_SIMPLE ("STATUS_STACK_OVERFLOW"); | |
a493e3e2 | 1105 | ourstatus->value.sig = GDB_SIGNAL_SEGV; |
7393af7c PM |
1106 | break; |
1107 | case STATUS_FLOAT_DENORMAL_OPERAND: | |
1108 | DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_DENORMAL_OPERAND"); | |
a493e3e2 | 1109 | ourstatus->value.sig = GDB_SIGNAL_FPE; |
7393af7c PM |
1110 | break; |
1111 | case EXCEPTION_ARRAY_BOUNDS_EXCEEDED: | |
1112 | DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_ARRAY_BOUNDS_EXCEEDED"); | |
a493e3e2 | 1113 | ourstatus->value.sig = GDB_SIGNAL_FPE; |
7393af7c PM |
1114 | break; |
1115 | case STATUS_FLOAT_INEXACT_RESULT: | |
1116 | DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_INEXACT_RESULT"); | |
a493e3e2 | 1117 | ourstatus->value.sig = GDB_SIGNAL_FPE; |
7393af7c PM |
1118 | break; |
1119 | case STATUS_FLOAT_INVALID_OPERATION: | |
1120 | DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_INVALID_OPERATION"); | |
a493e3e2 | 1121 | ourstatus->value.sig = GDB_SIGNAL_FPE; |
7393af7c PM |
1122 | break; |
1123 | case STATUS_FLOAT_OVERFLOW: | |
1124 | DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_OVERFLOW"); | |
a493e3e2 | 1125 | ourstatus->value.sig = GDB_SIGNAL_FPE; |
7393af7c PM |
1126 | break; |
1127 | case STATUS_FLOAT_STACK_CHECK: | |
1128 | DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_STACK_CHECK"); | |
a493e3e2 | 1129 | ourstatus->value.sig = GDB_SIGNAL_FPE; |
1ef980b9 | 1130 | break; |
3b7c8b74 | 1131 | case STATUS_FLOAT_UNDERFLOW: |
7393af7c | 1132 | DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_UNDERFLOW"); |
a493e3e2 | 1133 | ourstatus->value.sig = GDB_SIGNAL_FPE; |
7393af7c | 1134 | break; |
3b7c8b74 | 1135 | case STATUS_FLOAT_DIVIDE_BY_ZERO: |
7393af7c | 1136 | DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_DIVIDE_BY_ZERO"); |
a493e3e2 | 1137 | ourstatus->value.sig = GDB_SIGNAL_FPE; |
7393af7c | 1138 | break; |
3b7c8b74 | 1139 | case STATUS_INTEGER_DIVIDE_BY_ZERO: |
7393af7c | 1140 | DEBUG_EXCEPTION_SIMPLE ("STATUS_INTEGER_DIVIDE_BY_ZERO"); |
a493e3e2 | 1141 | ourstatus->value.sig = GDB_SIGNAL_FPE; |
3b7c8b74 | 1142 | break; |
7393af7c PM |
1143 | case STATUS_INTEGER_OVERFLOW: |
1144 | DEBUG_EXCEPTION_SIMPLE ("STATUS_INTEGER_OVERFLOW"); | |
a493e3e2 | 1145 | ourstatus->value.sig = GDB_SIGNAL_FPE; |
1ef980b9 SC |
1146 | break; |
1147 | case EXCEPTION_BREAKPOINT: | |
7393af7c | 1148 | DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_BREAKPOINT"); |
a493e3e2 | 1149 | ourstatus->value.sig = GDB_SIGNAL_TRAP; |
1ef980b9 SC |
1150 | break; |
1151 | case DBG_CONTROL_C: | |
7393af7c | 1152 | DEBUG_EXCEPTION_SIMPLE ("DBG_CONTROL_C"); |
a493e3e2 | 1153 | ourstatus->value.sig = GDB_SIGNAL_INT; |
5b421780 PM |
1154 | break; |
1155 | case DBG_CONTROL_BREAK: | |
7393af7c | 1156 | DEBUG_EXCEPTION_SIMPLE ("DBG_CONTROL_BREAK"); |
a493e3e2 | 1157 | ourstatus->value.sig = GDB_SIGNAL_INT; |
1ef980b9 SC |
1158 | break; |
1159 | case EXCEPTION_SINGLE_STEP: | |
7393af7c | 1160 | DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_SINGLE_STEP"); |
a493e3e2 | 1161 | ourstatus->value.sig = GDB_SIGNAL_TRAP; |
1ef980b9 | 1162 | break; |
8227c82d | 1163 | case EXCEPTION_ILLEGAL_INSTRUCTION: |
7393af7c | 1164 | DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_ILLEGAL_INSTRUCTION"); |
a493e3e2 | 1165 | ourstatus->value.sig = GDB_SIGNAL_ILL; |
7393af7c PM |
1166 | break; |
1167 | case EXCEPTION_PRIV_INSTRUCTION: | |
1168 | DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_PRIV_INSTRUCTION"); | |
a493e3e2 | 1169 | ourstatus->value.sig = GDB_SIGNAL_ILL; |
7393af7c PM |
1170 | break; |
1171 | case EXCEPTION_NONCONTINUABLE_EXCEPTION: | |
1172 | DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_NONCONTINUABLE_EXCEPTION"); | |
a493e3e2 | 1173 | ourstatus->value.sig = GDB_SIGNAL_ILL; |
8227c82d | 1174 | break; |
1ef980b9 | 1175 | default: |
581e13c1 | 1176 | /* Treat unhandled first chance exceptions specially. */ |
02e423b9 | 1177 | if (current_event.u.Exception.dwFirstChance) |
a244bdca | 1178 | return -1; |
d50a0ce2 CV |
1179 | printf_unfiltered ("gdb: unknown target exception 0x%08x at %s\n", |
1180 | (unsigned) current_event.u.Exception.ExceptionRecord.ExceptionCode, | |
a74ce742 PM |
1181 | host_address_to_string ( |
1182 | current_event.u.Exception.ExceptionRecord.ExceptionAddress)); | |
a493e3e2 | 1183 | ourstatus->value.sig = GDB_SIGNAL_UNKNOWN; |
1ef980b9 | 1184 | break; |
24e60978 | 1185 | } |
24e60978 | 1186 | exception_count++; |
7393af7c | 1187 | last_sig = ourstatus->value.sig; |
36339ecd | 1188 | return 1; |
24e60978 SC |
1189 | } |
1190 | ||
17617f2d EZ |
1191 | /* Resume thread specified by ID, or all artificially suspended |
1192 | threads, if we are continuing execution. KILLED non-zero means we | |
1193 | have killed the inferior, so we should ignore weird errors due to | |
1194 | threads shutting down. */ | |
3cee93ac | 1195 | static BOOL |
17617f2d | 1196 | windows_continue (DWORD continue_status, int id, int killed) |
3cee93ac CF |
1197 | { |
1198 | int i; | |
1199 | thread_info *th; | |
1200 | BOOL res; | |
1201 | ||
0c3d84be | 1202 | DEBUG_EVENTS (("ContinueDebugEvent (cpid=%d, ctid=0x%x, %s);\n", |
d50a0ce2 CV |
1203 | (unsigned) current_event.dwProcessId, |
1204 | (unsigned) current_event.dwThreadId, | |
dfe7f3ac | 1205 | continue_status == DBG_CONTINUE ? |
7393af7c | 1206 | "DBG_CONTINUE" : "DBG_EXCEPTION_NOT_HANDLED")); |
6537bb24 PA |
1207 | |
1208 | for (th = &thread_head; (th = th->next) != NULL;) | |
1209 | if ((id == -1 || id == (int) th->id) | |
1210 | && th->suspended) | |
1211 | { | |
1212 | if (debug_registers_changed) | |
1213 | { | |
1214 | th->context.ContextFlags |= CONTEXT_DEBUG_REGISTERS; | |
1215 | th->context.Dr0 = dr[0]; | |
1216 | th->context.Dr1 = dr[1]; | |
1217 | th->context.Dr2 = dr[2]; | |
1218 | th->context.Dr3 = dr[3]; | |
1219 | th->context.Dr6 = DR6_CLEAR_VALUE; | |
1220 | th->context.Dr7 = dr[7]; | |
1221 | } | |
1222 | if (th->context.ContextFlags) | |
1223 | { | |
17617f2d EZ |
1224 | DWORD ec = 0; |
1225 | ||
1226 | if (GetExitCodeThread (th->h, &ec) | |
1227 | && ec == STILL_ACTIVE) | |
1228 | { | |
1229 | BOOL status = SetThreadContext (th->h, &th->context); | |
1230 | ||
1231 | if (!killed) | |
1232 | CHECK (status); | |
1233 | } | |
6537bb24 PA |
1234 | th->context.ContextFlags = 0; |
1235 | } | |
1236 | if (th->suspended > 0) | |
1237 | (void) ResumeThread (th->h); | |
1238 | th->suspended = 0; | |
1239 | } | |
1240 | ||
0714f9bf SS |
1241 | res = ContinueDebugEvent (current_event.dwProcessId, |
1242 | current_event.dwThreadId, | |
1243 | continue_status); | |
3cee93ac | 1244 | |
fa4ba8da | 1245 | debug_registers_changed = 0; |
3cee93ac CF |
1246 | return res; |
1247 | } | |
1248 | ||
d6dc8049 CF |
1249 | /* Called in pathological case where Windows fails to send a |
1250 | CREATE_PROCESS_DEBUG_EVENT after an attach. */ | |
3ee6f623 | 1251 | static DWORD |
5439edaa | 1252 | fake_create_process (void) |
3ade5333 CF |
1253 | { |
1254 | current_process_handle = OpenProcess (PROCESS_ALL_ACCESS, FALSE, | |
1255 | current_event.dwProcessId); | |
bf25528d CF |
1256 | if (current_process_handle != NULL) |
1257 | open_process_used = 1; | |
1258 | else | |
1259 | { | |
d50a0ce2 CV |
1260 | error (_("OpenProcess call failed, GetLastError = %u"), |
1261 | (unsigned) GetLastError ()); | |
bf25528d CF |
1262 | /* We can not debug anything in that case. */ |
1263 | } | |
3ade5333 | 1264 | main_thread_id = current_event.dwThreadId; |
711e434b PM |
1265 | current_thread = windows_add_thread ( |
1266 | ptid_build (current_event.dwProcessId, 0, | |
1267 | current_event.dwThreadId), | |
1268 | current_event.u.CreateThread.hThread, | |
1269 | current_event.u.CreateThread.lpThreadLocalBase); | |
3ade5333 CF |
1270 | return main_thread_id; |
1271 | } | |
1272 | ||
a244bdca | 1273 | static void |
28439f5e | 1274 | windows_resume (struct target_ops *ops, |
2ea28649 | 1275 | ptid_t ptid, int step, enum gdb_signal sig) |
a244bdca CF |
1276 | { |
1277 | thread_info *th; | |
1278 | DWORD continue_status = DBG_CONTINUE; | |
1279 | ||
2dc38344 PA |
1280 | /* A specific PTID means `step only this thread id'. */ |
1281 | int resume_all = ptid_equal (ptid, minus_one_ptid); | |
1282 | ||
1283 | /* If we're continuing all threads, it's the current inferior that | |
1284 | should be handled specially. */ | |
1285 | if (resume_all) | |
1286 | ptid = inferior_ptid; | |
a244bdca | 1287 | |
a493e3e2 | 1288 | if (sig != GDB_SIGNAL_0) |
a244bdca CF |
1289 | { |
1290 | if (current_event.dwDebugEventCode != EXCEPTION_DEBUG_EVENT) | |
1291 | { | |
1292 | DEBUG_EXCEPT(("Cannot continue with signal %d here.\n",sig)); | |
1293 | } | |
1294 | else if (sig == last_sig) | |
1295 | continue_status = DBG_EXCEPTION_NOT_HANDLED; | |
1296 | else | |
1297 | #if 0 | |
1298 | /* This code does not seem to work, because | |
1299 | the kernel does probably not consider changes in the ExceptionRecord | |
1300 | structure when passing the exception to the inferior. | |
1301 | Note that this seems possible in the exception handler itself. */ | |
1302 | { | |
1303 | int i; | |
1304 | for (i = 0; xlate[i].them != -1; i++) | |
1305 | if (xlate[i].us == sig) | |
1306 | { | |
581e13c1 MS |
1307 | current_event.u.Exception.ExceptionRecord.ExceptionCode |
1308 | = xlate[i].them; | |
a244bdca CF |
1309 | continue_status = DBG_EXCEPTION_NOT_HANDLED; |
1310 | break; | |
1311 | } | |
1312 | if (continue_status == DBG_CONTINUE) | |
1313 | { | |
1314 | DEBUG_EXCEPT(("Cannot continue with signal %d.\n",sig)); | |
1315 | } | |
1316 | } | |
1317 | #endif | |
1318 | DEBUG_EXCEPT(("Can only continue with recieved signal %d.\n", | |
1319 | last_sig)); | |
1320 | } | |
1321 | ||
a493e3e2 | 1322 | last_sig = GDB_SIGNAL_0; |
a244bdca | 1323 | |
dc05df57 | 1324 | DEBUG_EXEC (("gdb: windows_resume (pid=%d, tid=%ld, step=%d, sig=%d);\n", |
2dc38344 | 1325 | ptid_get_pid (ptid), ptid_get_tid (ptid), step, sig)); |
a244bdca | 1326 | |
581e13c1 | 1327 | /* Get context for currently selected thread. */ |
2dc38344 | 1328 | th = thread_rec (ptid_get_tid (inferior_ptid), FALSE); |
a244bdca CF |
1329 | if (th) |
1330 | { | |
1331 | if (step) | |
1332 | { | |
581e13c1 | 1333 | /* Single step by setting t bit. */ |
a97b0ac8 UW |
1334 | struct regcache *regcache = get_current_regcache (); |
1335 | struct gdbarch *gdbarch = get_regcache_arch (regcache); | |
1336 | windows_fetch_inferior_registers (ops, regcache, | |
1337 | gdbarch_ps_regnum (gdbarch)); | |
a244bdca CF |
1338 | th->context.EFlags |= FLAG_TRACE_BIT; |
1339 | } | |
1340 | ||
1341 | if (th->context.ContextFlags) | |
1342 | { | |
1343 | if (debug_registers_changed) | |
1344 | { | |
1345 | th->context.Dr0 = dr[0]; | |
1346 | th->context.Dr1 = dr[1]; | |
1347 | th->context.Dr2 = dr[2]; | |
1348 | th->context.Dr3 = dr[3]; | |
6537bb24 | 1349 | th->context.Dr6 = DR6_CLEAR_VALUE; |
a244bdca CF |
1350 | th->context.Dr7 = dr[7]; |
1351 | } | |
1352 | CHECK (SetThreadContext (th->h, &th->context)); | |
1353 | th->context.ContextFlags = 0; | |
1354 | } | |
1355 | } | |
1356 | ||
1357 | /* Allow continuing with the same signal that interrupted us. | |
581e13c1 | 1358 | Otherwise complain. */ |
a244bdca | 1359 | |
2dc38344 | 1360 | if (resume_all) |
17617f2d | 1361 | windows_continue (continue_status, -1, 0); |
2dc38344 | 1362 | else |
17617f2d | 1363 | windows_continue (continue_status, ptid_get_tid (ptid), 0); |
a244bdca CF |
1364 | } |
1365 | ||
695de547 CF |
1366 | /* Ctrl-C handler used when the inferior is not run in the same console. The |
1367 | handler is in charge of interrupting the inferior using DebugBreakProcess. | |
1368 | Note that this function is not available prior to Windows XP. In this case | |
1369 | we emit a warning. */ | |
d603d4b3 | 1370 | static BOOL WINAPI |
695de547 CF |
1371 | ctrl_c_handler (DWORD event_type) |
1372 | { | |
1373 | const int attach_flag = current_inferior ()->attach_flag; | |
1374 | ||
bb0613a5 PM |
1375 | /* Only handle Ctrl-C and Ctrl-Break events. Ignore others. */ |
1376 | if (event_type != CTRL_C_EVENT && event_type != CTRL_BREAK_EVENT) | |
695de547 CF |
1377 | return FALSE; |
1378 | ||
1379 | /* If the inferior and the debugger share the same console, do nothing as | |
1380 | the inferior has also received the Ctrl-C event. */ | |
1381 | if (!new_console && !attach_flag) | |
1382 | return TRUE; | |
1383 | ||
1384 | if (!DebugBreakProcess (current_process_handle)) | |
581e13c1 MS |
1385 | warning (_("Could not interrupt program. " |
1386 | "Press Ctrl-c in the program console.")); | |
695de547 CF |
1387 | |
1388 | /* Return true to tell that Ctrl-C has been handled. */ | |
1389 | return TRUE; | |
1390 | } | |
1391 | ||
8a892701 | 1392 | /* Get the next event from the child. Return 1 if the event requires |
695de547 | 1393 | handling by WFI (or whatever). */ |
1e37c281 | 1394 | static int |
28439f5e PA |
1395 | get_windows_debug_event (struct target_ops *ops, |
1396 | int pid, struct target_waitstatus *ourstatus) | |
1e37c281 JM |
1397 | { |
1398 | BOOL debug_event; | |
8a892701 | 1399 | DWORD continue_status, event_code; |
87a45c96 | 1400 | thread_info *th; |
8a892701 | 1401 | static thread_info dummy_thread_info; |
450005e7 | 1402 | int retval = 0; |
1e37c281 | 1403 | |
a493e3e2 | 1404 | last_sig = GDB_SIGNAL_0; |
9d3789f7 | 1405 | |
8a892701 | 1406 | if (!(debug_event = WaitForDebugEvent (¤t_event, 1000))) |
29fe111d | 1407 | goto out; |
1e37c281 JM |
1408 | |
1409 | event_count++; | |
1410 | continue_status = DBG_CONTINUE; | |
1e37c281 | 1411 | |
8a892701 | 1412 | event_code = current_event.dwDebugEventCode; |
450005e7 | 1413 | ourstatus->kind = TARGET_WAITKIND_SPURIOUS; |
87a45c96 | 1414 | th = NULL; |
a244bdca | 1415 | have_saved_context = 0; |
8a892701 CF |
1416 | |
1417 | switch (event_code) | |
1e37c281 JM |
1418 | { |
1419 | case CREATE_THREAD_DEBUG_EVENT: | |
0c3d84be | 1420 | DEBUG_EVENTS (("gdb: kernel event for pid=%u tid=0x%x code=%s)\n", |
8a892701 CF |
1421 | (unsigned) current_event.dwProcessId, |
1422 | (unsigned) current_event.dwThreadId, | |
1423 | "CREATE_THREAD_DEBUG_EVENT")); | |
dfe7f3ac | 1424 | if (saw_create != 1) |
3ade5333 | 1425 | { |
181e7f93 PA |
1426 | struct inferior *inf; |
1427 | inf = find_inferior_pid (current_event.dwProcessId); | |
1428 | if (!saw_create && inf->attach_flag) | |
3ade5333 | 1429 | { |
d6dc8049 CF |
1430 | /* Kludge around a Windows bug where first event is a create |
1431 | thread event. Caused when attached process does not have | |
581e13c1 | 1432 | a main thread. */ |
3a3e9ee3 | 1433 | retval = fake_create_process (); |
181e7f93 PA |
1434 | if (retval) |
1435 | saw_create++; | |
3ade5333 CF |
1436 | } |
1437 | break; | |
1438 | } | |
581e13c1 | 1439 | /* Record the existence of this thread. */ |
450005e7 | 1440 | retval = current_event.dwThreadId; |
dc05df57 | 1441 | th = windows_add_thread (ptid_build (current_event.dwProcessId, 0, |
2dc38344 | 1442 | current_event.dwThreadId), |
711e434b PM |
1443 | current_event.u.CreateThread.hThread, |
1444 | current_event.u.CreateThread.lpThreadLocalBase); | |
1445 | ||
1e37c281 JM |
1446 | break; |
1447 | ||
1448 | case EXIT_THREAD_DEBUG_EVENT: | |
0c3d84be | 1449 | DEBUG_EVENTS (("gdb: kernel event for pid=%u tid=0x%x code=%s)\n", |
8a892701 CF |
1450 | (unsigned) current_event.dwProcessId, |
1451 | (unsigned) current_event.dwThreadId, | |
1452 | "EXIT_THREAD_DEBUG_EVENT")); | |
b3c613f2 | 1453 | |
87a45c96 CF |
1454 | if (current_event.dwThreadId != main_thread_id) |
1455 | { | |
dc05df57 | 1456 | windows_delete_thread (ptid_build (current_event.dwProcessId, 0, |
e0ea48a0 EZ |
1457 | current_event.dwThreadId), |
1458 | current_event.u.ExitThread.dwExitCode); | |
87a45c96 CF |
1459 | th = &dummy_thread_info; |
1460 | } | |
1e37c281 JM |
1461 | break; |
1462 | ||
1463 | case CREATE_PROCESS_DEBUG_EVENT: | |
0c3d84be | 1464 | DEBUG_EVENTS (("gdb: kernel event for pid=%u tid=0x%x code=%s)\n", |
8a892701 CF |
1465 | (unsigned) current_event.dwProcessId, |
1466 | (unsigned) current_event.dwThreadId, | |
1467 | "CREATE_PROCESS_DEBUG_EVENT")); | |
700b351b | 1468 | CloseHandle (current_event.u.CreateProcessInfo.hFile); |
dfe7f3ac | 1469 | if (++saw_create != 1) |
bf25528d | 1470 | break; |
1e37c281 | 1471 | |
dfe7f3ac | 1472 | current_process_handle = current_event.u.CreateProcessInfo.hProcess; |
87a45c96 | 1473 | if (main_thread_id) |
695de547 | 1474 | windows_delete_thread (ptid_build (current_event.dwProcessId, 0, |
e0ea48a0 EZ |
1475 | main_thread_id), |
1476 | 0); | |
9d3789f7 | 1477 | main_thread_id = current_event.dwThreadId; |
581e13c1 | 1478 | /* Add the main thread. */ |
dc05df57 | 1479 | th = windows_add_thread (ptid_build (current_event.dwProcessId, 0, |
695de547 | 1480 | current_event.dwThreadId), |
711e434b PM |
1481 | current_event.u.CreateProcessInfo.hThread, |
1482 | current_event.u.CreateProcessInfo.lpThreadLocalBase); | |
3a3e9ee3 | 1483 | retval = current_event.dwThreadId; |
1e37c281 JM |
1484 | break; |
1485 | ||
1486 | case EXIT_PROCESS_DEBUG_EVENT: | |
0c3d84be | 1487 | DEBUG_EVENTS (("gdb: kernel event for pid=%u tid=0x%x code=%s)\n", |
8a892701 CF |
1488 | (unsigned) current_event.dwProcessId, |
1489 | (unsigned) current_event.dwThreadId, | |
1490 | "EXIT_PROCESS_DEBUG_EVENT")); | |
16d905e2 CF |
1491 | if (!windows_initialization_done) |
1492 | { | |
1493 | target_terminal_ours (); | |
1494 | target_mourn_inferior (); | |
1495 | error (_("During startup program exited with code 0x%x."), | |
1496 | (unsigned int) current_event.u.ExitProcess.dwExitCode); | |
1497 | } | |
1498 | else if (saw_create == 1) | |
1499 | { | |
1500 | ourstatus->kind = TARGET_WAITKIND_EXITED; | |
1501 | ourstatus->value.integer = current_event.u.ExitProcess.dwExitCode; | |
1502 | retval = main_thread_id; | |
1503 | } | |
8a892701 | 1504 | break; |
1e37c281 JM |
1505 | |
1506 | case LOAD_DLL_DEBUG_EVENT: | |
0c3d84be | 1507 | DEBUG_EVENTS (("gdb: kernel event for pid=%u tid=0x%x code=%s)\n", |
8a892701 CF |
1508 | (unsigned) current_event.dwProcessId, |
1509 | (unsigned) current_event.dwThreadId, | |
1510 | "LOAD_DLL_DEBUG_EVENT")); | |
700b351b | 1511 | CloseHandle (current_event.u.LoadDll.hFile); |
ea39ad35 | 1512 | if (saw_create != 1 || ! windows_initialization_done) |
dfe7f3ac | 1513 | break; |
8a892701 | 1514 | catch_errors (handle_load_dll, NULL, (char *) "", RETURN_MASK_ALL); |
450005e7 CF |
1515 | ourstatus->kind = TARGET_WAITKIND_LOADED; |
1516 | ourstatus->value.integer = 0; | |
9d3789f7 | 1517 | retval = main_thread_id; |
1e37c281 JM |
1518 | break; |
1519 | ||
1520 | case UNLOAD_DLL_DEBUG_EVENT: | |
0c3d84be | 1521 | DEBUG_EVENTS (("gdb: kernel event for pid=%u tid=0x%x code=%s)\n", |
8a892701 CF |
1522 | (unsigned) current_event.dwProcessId, |
1523 | (unsigned) current_event.dwThreadId, | |
1524 | "UNLOAD_DLL_DEBUG_EVENT")); | |
ea39ad35 | 1525 | if (saw_create != 1 || ! windows_initialization_done) |
dfe7f3ac | 1526 | break; |
d3ff4a77 | 1527 | catch_errors (handle_unload_dll, NULL, (char *) "", RETURN_MASK_ALL); |
de1b3c3d PA |
1528 | ourstatus->kind = TARGET_WAITKIND_LOADED; |
1529 | ourstatus->value.integer = 0; | |
1530 | retval = main_thread_id; | |
d3ff4a77 | 1531 | break; |
1e37c281 JM |
1532 | |
1533 | case EXCEPTION_DEBUG_EVENT: | |
0c3d84be | 1534 | DEBUG_EVENTS (("gdb: kernel event for pid=%u tid=0x%x code=%s)\n", |
8a892701 CF |
1535 | (unsigned) current_event.dwProcessId, |
1536 | (unsigned) current_event.dwThreadId, | |
1537 | "EXCEPTION_DEBUG_EVENT")); | |
dfe7f3ac CF |
1538 | if (saw_create != 1) |
1539 | break; | |
a244bdca CF |
1540 | switch (handle_exception (ourstatus)) |
1541 | { | |
1542 | case 0: | |
1543 | continue_status = DBG_EXCEPTION_NOT_HANDLED; | |
1544 | break; | |
1545 | case 1: | |
1546 | retval = current_event.dwThreadId; | |
1547 | break; | |
1548 | case -1: | |
1549 | last_sig = 1; | |
1550 | continue_status = -1; | |
1551 | break; | |
1552 | } | |
1e37c281 JM |
1553 | break; |
1554 | ||
581e13c1 | 1555 | case OUTPUT_DEBUG_STRING_EVENT: /* Message from the kernel. */ |
0c3d84be | 1556 | DEBUG_EVENTS (("gdb: kernel event for pid=%u tid=0x%x code=%s)\n", |
8a892701 CF |
1557 | (unsigned) current_event.dwProcessId, |
1558 | (unsigned) current_event.dwThreadId, | |
1559 | "OUTPUT_DEBUG_STRING_EVENT")); | |
dfe7f3ac CF |
1560 | if (saw_create != 1) |
1561 | break; | |
a244bdca | 1562 | retval = handle_output_debug_string (ourstatus); |
1e37c281 | 1563 | break; |
9d3789f7 | 1564 | |
1e37c281 | 1565 | default: |
dfe7f3ac CF |
1566 | if (saw_create != 1) |
1567 | break; | |
0c3d84be | 1568 | printf_unfiltered ("gdb: kernel event for pid=%u tid=0x%x\n", |
d50a0ce2 CV |
1569 | (unsigned) current_event.dwProcessId, |
1570 | (unsigned) current_event.dwThreadId); | |
1571 | printf_unfiltered (" unknown event code %u\n", | |
1572 | (unsigned) current_event.dwDebugEventCode); | |
1e37c281 JM |
1573 | break; |
1574 | } | |
1575 | ||
dfe7f3ac | 1576 | if (!retval || saw_create != 1) |
a244bdca CF |
1577 | { |
1578 | if (continue_status == -1) | |
02529b48 | 1579 | windows_resume (ops, minus_one_ptid, 0, 1); |
a244bdca | 1580 | else |
17617f2d | 1581 | CHECK (windows_continue (continue_status, -1, 0)); |
a244bdca | 1582 | } |
450005e7 | 1583 | else |
9d3789f7 | 1584 | { |
2dc38344 PA |
1585 | inferior_ptid = ptid_build (current_event.dwProcessId, 0, |
1586 | retval); | |
3ade5333 | 1587 | current_thread = th ?: thread_rec (current_event.dwThreadId, TRUE); |
9d3789f7 | 1588 | } |
1e37c281 JM |
1589 | |
1590 | out: | |
450005e7 | 1591 | return retval; |
1e37c281 JM |
1592 | } |
1593 | ||
2dc38344 | 1594 | /* Wait for interesting events to occur in the target process. */ |
39f77062 | 1595 | static ptid_t |
117de6a9 | 1596 | windows_wait (struct target_ops *ops, |
47608cb1 | 1597 | ptid_t ptid, struct target_waitstatus *ourstatus, int options) |
24e60978 | 1598 | { |
2dc38344 | 1599 | int pid = -1; |
39f77062 | 1600 | |
c44537cf CV |
1601 | target_terminal_ours (); |
1602 | ||
24e60978 SC |
1603 | /* We loop when we get a non-standard exception rather than return |
1604 | with a SPURIOUS because resume can try and step or modify things, | |
3cee93ac | 1605 | which needs a current_thread->h. But some of these exceptions mark |
24e60978 | 1606 | the birth or death of threads, which mean that the current thread |
581e13c1 | 1607 | isn't necessarily what you think it is. */ |
24e60978 SC |
1608 | |
1609 | while (1) | |
450005e7 | 1610 | { |
c57918b2 | 1611 | int retval; |
2b008701 | 1612 | |
695de547 CF |
1613 | /* If the user presses Ctrl-c while the debugger is waiting |
1614 | for an event, he expects the debugger to interrupt his program | |
1615 | and to get the prompt back. There are two possible situations: | |
1616 | ||
1617 | - The debugger and the program do not share the console, in | |
1618 | which case the Ctrl-c event only reached the debugger. | |
1619 | In that case, the ctrl_c handler will take care of interrupting | |
581e13c1 MS |
1620 | the inferior. Note that this case is working starting with |
1621 | Windows XP. For Windows 2000, Ctrl-C should be pressed in the | |
695de547 CF |
1622 | inferior console. |
1623 | ||
1624 | - The debugger and the program share the same console, in which | |
1625 | case both debugger and inferior will receive the Ctrl-c event. | |
1626 | In that case the ctrl_c handler will ignore the event, as the | |
1627 | Ctrl-c event generated inside the inferior will trigger the | |
1628 | expected debug event. | |
1629 | ||
1630 | FIXME: brobecker/2008-05-20: If the inferior receives the | |
1631 | signal first and the delay until GDB receives that signal | |
1632 | is sufficiently long, GDB can sometimes receive the SIGINT | |
1633 | after we have unblocked the CTRL+C handler. This would | |
1634 | lead to the debugger stopping prematurely while handling | |
1635 | the new-thread event that comes with the handling of the SIGINT | |
1636 | inside the inferior, and then stop again immediately when | |
1637 | the user tries to resume the execution in the inferior. | |
1638 | This is a classic race that we should try to fix one day. */ | |
1639 | SetConsoleCtrlHandler (&ctrl_c_handler, TRUE); | |
28439f5e | 1640 | retval = get_windows_debug_event (ops, pid, ourstatus); |
695de547 | 1641 | SetConsoleCtrlHandler (&ctrl_c_handler, FALSE); |
c57918b2 | 1642 | |
450005e7 | 1643 | if (retval) |
2dc38344 | 1644 | return ptid_build (current_event.dwProcessId, 0, retval); |
450005e7 CF |
1645 | else |
1646 | { | |
1647 | int detach = 0; | |
3cee93ac | 1648 | |
98bbd631 AC |
1649 | if (deprecated_ui_loop_hook != NULL) |
1650 | detach = deprecated_ui_loop_hook (0); | |
0714f9bf | 1651 | |
450005e7 | 1652 | if (detach) |
7d85a9c0 | 1653 | windows_kill_inferior (ops); |
450005e7 CF |
1654 | } |
1655 | } | |
24e60978 SC |
1656 | } |
1657 | ||
ea39ad35 JB |
1658 | /* Iterate over all DLLs currently mapped by our inferior, and |
1659 | add them to our list of solibs. */ | |
94481b8c JB |
1660 | |
1661 | static void | |
ea39ad35 | 1662 | windows_add_all_dlls (void) |
94481b8c JB |
1663 | { |
1664 | struct so_list *so; | |
1665 | HMODULE dummy_hmodule; | |
1666 | DWORD cb_needed; | |
1667 | HMODULE *hmodules; | |
1668 | int i; | |
1669 | ||
94481b8c JB |
1670 | if (EnumProcessModules (current_process_handle, &dummy_hmodule, |
1671 | sizeof (HMODULE), &cb_needed) == 0) | |
1672 | return; | |
1673 | ||
1674 | if (cb_needed < 1) | |
1675 | return; | |
1676 | ||
1677 | hmodules = (HMODULE *) alloca (cb_needed); | |
1678 | if (EnumProcessModules (current_process_handle, hmodules, | |
1679 | cb_needed, &cb_needed) == 0) | |
1680 | return; | |
1681 | ||
ea39ad35 | 1682 | for (i = 1; i < (int) (cb_needed / sizeof (HMODULE)); i++) |
94481b8c JB |
1683 | { |
1684 | MODULEINFO mi; | |
774f74c2 PM |
1685 | #ifdef __USEWIDE |
1686 | wchar_t dll_name[__PMAX]; | |
1687 | char name[__PMAX]; | |
1688 | #else | |
94481b8c | 1689 | char dll_name[__PMAX]; |
774f74c2 PM |
1690 | char *name; |
1691 | #endif | |
94481b8c JB |
1692 | if (GetModuleInformation (current_process_handle, hmodules[i], |
1693 | &mi, sizeof (mi)) == 0) | |
1694 | continue; | |
1695 | if (GetModuleFileNameEx (current_process_handle, hmodules[i], | |
1696 | dll_name, sizeof (dll_name)) == 0) | |
1697 | continue; | |
774f74c2 PM |
1698 | #ifdef __USEWIDE |
1699 | wcstombs (name, dll_name, __PMAX); | |
1700 | #else | |
1701 | name = dll_name; | |
1702 | #endif | |
ea39ad35 JB |
1703 | |
1704 | solib_end->next = windows_make_so (name, mi.lpBaseOfDll); | |
1705 | solib_end = solib_end->next; | |
94481b8c JB |
1706 | } |
1707 | } | |
1708 | ||
9d3789f7 | 1709 | static void |
dc05df57 | 1710 | do_initial_windows_stuff (struct target_ops *ops, DWORD pid, int attaching) |
9d3789f7 CF |
1711 | { |
1712 | extern int stop_after_trap; | |
fa4ba8da | 1713 | int i; |
d6b48e9c | 1714 | struct inferior *inf; |
2020b7ab | 1715 | struct thread_info *tp; |
9d3789f7 | 1716 | |
a493e3e2 | 1717 | last_sig = GDB_SIGNAL_0; |
9d3789f7 CF |
1718 | event_count = 0; |
1719 | exception_count = 0; | |
bf25528d | 1720 | open_process_used = 0; |
fa4ba8da | 1721 | debug_registers_changed = 0; |
dfe7f3ac | 1722 | debug_registers_used = 0; |
fa4ba8da PM |
1723 | for (i = 0; i < sizeof (dr) / sizeof (dr[0]); i++) |
1724 | dr[i] = 0; | |
10325bc5 | 1725 | #ifdef __CYGWIN__ |
de1b3c3d | 1726 | cygwin_load_start = cygwin_load_end = 0; |
10325bc5 | 1727 | #endif |
9d3789f7 CF |
1728 | current_event.dwProcessId = pid; |
1729 | memset (¤t_event, 0, sizeof (current_event)); | |
6a3cb8e8 PA |
1730 | if (!target_is_pushed (ops)) |
1731 | push_target (ops); | |
cb851954 | 1732 | disable_breakpoints_in_shlibs (); |
dc05df57 | 1733 | windows_clear_solib (); |
9d3789f7 CF |
1734 | clear_proceed_status (); |
1735 | init_wait_for_inferior (); | |
1736 | ||
6c95b8df PA |
1737 | inf = current_inferior (); |
1738 | inferior_appeared (inf, pid); | |
181e7f93 | 1739 | inf->attach_flag = attaching; |
7f9f62ba | 1740 | |
9f9d052e PM |
1741 | /* Make the new process the current inferior, so terminal handling |
1742 | can rely on it. When attaching, we don't know about any thread | |
1743 | id here, but that's OK --- nothing should be referencing the | |
dc05df57 | 1744 | current thread until we report an event out of windows_wait. */ |
9f9d052e PM |
1745 | inferior_ptid = pid_to_ptid (pid); |
1746 | ||
d6b64346 | 1747 | child_terminal_init_with_pgrp (pid); |
9d3789f7 CF |
1748 | target_terminal_inferior (); |
1749 | ||
16d905e2 | 1750 | windows_initialization_done = 0; |
16c381f0 | 1751 | inf->control.stop_soon = STOP_QUIETLY; |
9d3789f7 CF |
1752 | while (1) |
1753 | { | |
1754 | stop_after_trap = 1; | |
e4c8541f | 1755 | wait_for_inferior (); |
2020b7ab | 1756 | tp = inferior_thread (); |
a493e3e2 | 1757 | if (tp->suspend.stop_signal != GDB_SIGNAL_TRAP) |
16c381f0 | 1758 | resume (0, tp->suspend.stop_signal); |
9d3789f7 CF |
1759 | else |
1760 | break; | |
1761 | } | |
eff8332b | 1762 | |
ea39ad35 | 1763 | /* Now that the inferior has been started and all DLLs have been mapped, |
3be75f87 JB |
1764 | we can iterate over all DLLs and load them in. |
1765 | ||
1766 | We avoid doing it any earlier because, on certain versions of Windows, | |
1767 | LOAD_DLL_DEBUG_EVENTs are sometimes not complete. In particular, | |
1768 | we have seen on Windows 8.1 that the ntdll.dll load event does not | |
1769 | include the DLL name, preventing us from creating an associated SO. | |
1770 | A possible explanation is that ntdll.dll might be mapped before | |
1771 | the SO info gets created by the Windows system -- ntdll.dll is | |
1772 | the first DLL to be reported via LOAD_DLL_DEBUG_EVENT and other DLLs | |
1773 | do not seem to suffer from that problem. | |
1774 | ||
1775 | Rather than try to work around this sort of issue, it is much | |
1776 | simpler to just ignore DLL load/unload events during the startup | |
1777 | phase, and then process them all in one batch now. */ | |
ea39ad35 | 1778 | windows_add_all_dlls (); |
94481b8c | 1779 | |
16d905e2 | 1780 | windows_initialization_done = 1; |
16c381f0 | 1781 | inf->control.stop_soon = NO_STOP_QUIETLY; |
9d3789f7 CF |
1782 | stop_after_trap = 0; |
1783 | return; | |
1784 | } | |
1785 | ||
616a9dc4 CV |
1786 | /* Try to set or remove a user privilege to the current process. Return -1 |
1787 | if that fails, the previous setting of that privilege otherwise. | |
1788 | ||
1789 | This code is copied from the Cygwin source code and rearranged to allow | |
1790 | dynamically loading of the needed symbols from advapi32 which is only | |
581e13c1 | 1791 | available on NT/2K/XP. */ |
616a9dc4 CV |
1792 | static int |
1793 | set_process_privilege (const char *privilege, BOOL enable) | |
1794 | { | |
616a9dc4 CV |
1795 | HANDLE token_hdl = NULL; |
1796 | LUID restore_priv; | |
1797 | TOKEN_PRIVILEGES new_priv, orig_priv; | |
1798 | int ret = -1; | |
1799 | DWORD size; | |
1800 | ||
616a9dc4 CV |
1801 | if (!OpenProcessToken (GetCurrentProcess (), |
1802 | TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES, | |
1803 | &token_hdl)) | |
1804 | goto out; | |
1805 | ||
418c6cb3 | 1806 | if (!LookupPrivilegeValueA (NULL, privilege, &restore_priv)) |
616a9dc4 CV |
1807 | goto out; |
1808 | ||
1809 | new_priv.PrivilegeCount = 1; | |
1810 | new_priv.Privileges[0].Luid = restore_priv; | |
1811 | new_priv.Privileges[0].Attributes = enable ? SE_PRIVILEGE_ENABLED : 0; | |
1812 | ||
1813 | if (!AdjustTokenPrivileges (token_hdl, FALSE, &new_priv, | |
295732ea | 1814 | sizeof orig_priv, &orig_priv, &size)) |
616a9dc4 CV |
1815 | goto out; |
1816 | #if 0 | |
1817 | /* Disabled, otherwise every `attach' in an unprivileged user session | |
1818 | would raise the "Failed to get SE_DEBUG_NAME privilege" warning in | |
581e13c1 | 1819 | windows_attach(). */ |
616a9dc4 | 1820 | /* AdjustTokenPrivileges returns TRUE even if the privilege could not |
581e13c1 | 1821 | be enabled. GetLastError () returns an correct error code, though. */ |
616a9dc4 CV |
1822 | if (enable && GetLastError () == ERROR_NOT_ALL_ASSIGNED) |
1823 | goto out; | |
1824 | #endif | |
1825 | ||
1826 | ret = orig_priv.Privileges[0].Attributes == SE_PRIVILEGE_ENABLED ? 1 : 0; | |
1827 | ||
1828 | out: | |
1829 | if (token_hdl) | |
1830 | CloseHandle (token_hdl); | |
1831 | ||
1832 | return ret; | |
1833 | } | |
1834 | ||
02cc9f49 | 1835 | /* Attach to process PID, then initialize for debugging it. */ |
24e60978 | 1836 | static void |
dc05df57 | 1837 | windows_attach (struct target_ops *ops, char *args, int from_tty) |
24e60978 SC |
1838 | { |
1839 | BOOL ok; | |
559e75c0 | 1840 | DWORD pid; |
24e60978 | 1841 | |
74164c56 | 1842 | pid = parse_pid_to_attach (args); |
24e60978 | 1843 | |
616a9dc4 CV |
1844 | if (set_process_privilege (SE_DEBUG_NAME, TRUE) < 0) |
1845 | { | |
1846 | printf_unfiltered ("Warning: Failed to get SE_DEBUG_NAME privilege\n"); | |
581e13c1 MS |
1847 | printf_unfiltered ("This can cause attach to " |
1848 | "fail on Windows NT/2K/XP\n"); | |
616a9dc4 CV |
1849 | } |
1850 | ||
dc05df57 | 1851 | windows_init_thread_list (); |
9d3789f7 | 1852 | ok = DebugActiveProcess (pid); |
91a175b3 | 1853 | saw_create = 0; |
24e60978 | 1854 | |
10325bc5 | 1855 | #ifdef __CYGWIN__ |
24e60978 | 1856 | if (!ok) |
baa93fa6 | 1857 | { |
581e13c1 | 1858 | /* Try fall back to Cygwin pid. */ |
baa93fa6 CF |
1859 | pid = cygwin_internal (CW_CYGWIN_PID_TO_WINPID, pid); |
1860 | ||
1861 | if (pid > 0) | |
1862 | ok = DebugActiveProcess (pid); | |
10325bc5 PA |
1863 | } |
1864 | #endif | |
baa93fa6 | 1865 | |
10325bc5 PA |
1866 | if (!ok) |
1867 | error (_("Can't attach to process.")); | |
24e60978 | 1868 | |
2b008701 | 1869 | DebugSetProcessKillOnExit (FALSE); |
3ade5333 | 1870 | |
24e60978 SC |
1871 | if (from_tty) |
1872 | { | |
1873 | char *exec_file = (char *) get_exec_file (0); | |
1874 | ||
1875 | if (exec_file) | |
1876 | printf_unfiltered ("Attaching to program `%s', %s\n", exec_file, | |
39f77062 | 1877 | target_pid_to_str (pid_to_ptid (pid))); |
24e60978 SC |
1878 | else |
1879 | printf_unfiltered ("Attaching to %s\n", | |
39f77062 | 1880 | target_pid_to_str (pid_to_ptid (pid))); |
24e60978 SC |
1881 | |
1882 | gdb_flush (gdb_stdout); | |
1883 | } | |
1884 | ||
dc05df57 | 1885 | do_initial_windows_stuff (ops, pid, 1); |
9d3789f7 | 1886 | target_terminal_ours (); |
24e60978 SC |
1887 | } |
1888 | ||
24e60978 | 1889 | static void |
52554a0e | 1890 | windows_detach (struct target_ops *ops, const char *args, int from_tty) |
24e60978 | 1891 | { |
02cc9f49 CV |
1892 | int detached = 1; |
1893 | ||
2b008701 | 1894 | ptid_t ptid = {-1}; |
a493e3e2 | 1895 | windows_resume (ops, ptid, 0, GDB_SIGNAL_0); |
96998ce7 | 1896 | |
2b008701 CF |
1897 | if (!DebugActiveProcessStop (current_event.dwProcessId)) |
1898 | { | |
d50a0ce2 CV |
1899 | error (_("Can't detach process %u (error %u)"), |
1900 | (unsigned) current_event.dwProcessId, (unsigned) GetLastError ()); | |
2b008701 | 1901 | detached = 0; |
02cc9f49 | 1902 | } |
2b008701 CF |
1903 | DebugSetProcessKillOnExit (FALSE); |
1904 | ||
02cc9f49 | 1905 | if (detached && from_tty) |
24e60978 SC |
1906 | { |
1907 | char *exec_file = get_exec_file (0); | |
1908 | if (exec_file == 0) | |
1909 | exec_file = ""; | |
d50a0ce2 CV |
1910 | printf_unfiltered ("Detaching from program: %s, Pid %u\n", exec_file, |
1911 | (unsigned) current_event.dwProcessId); | |
24e60978 SC |
1912 | gdb_flush (gdb_stdout); |
1913 | } | |
7f9f62ba | 1914 | |
26cb8b7c | 1915 | i386_cleanup_dregs (); |
39f77062 | 1916 | inferior_ptid = null_ptid; |
7f9f62ba PA |
1917 | detach_inferior (current_event.dwProcessId); |
1918 | ||
6a3cb8e8 | 1919 | inf_child_maybe_unpush_target (ops); |
24e60978 SC |
1920 | } |
1921 | ||
47f7ffdb JB |
1922 | /* Try to determine the executable filename. |
1923 | ||
1924 | EXE_NAME_RET is a pointer to a buffer whose size is EXE_NAME_MAX_LEN. | |
1925 | ||
1926 | Upon success, the filename is stored inside EXE_NAME_RET, and | |
1927 | this function returns nonzero. | |
1928 | ||
1929 | Otherwise, this function returns zero and the contents of | |
1930 | EXE_NAME_RET is undefined. */ | |
1931 | ||
1932 | static int | |
1933 | windows_get_exec_module_filename (char *exe_name_ret, size_t exe_name_max_len) | |
1934 | { | |
1935 | DWORD len; | |
1936 | HMODULE dh_buf; | |
1937 | DWORD cbNeeded; | |
1938 | ||
1939 | cbNeeded = 0; | |
1940 | if (!EnumProcessModules (current_process_handle, &dh_buf, | |
1941 | sizeof (HMODULE), &cbNeeded) || !cbNeeded) | |
1942 | return 0; | |
1943 | ||
1944 | /* We know the executable is always first in the list of modules, | |
1945 | which we just fetched. So no need to fetch more. */ | |
1946 | ||
1947 | #ifdef __CYGWIN__ | |
1948 | { | |
1949 | /* Cygwin prefers that the path be in /x/y/z format, so extract | |
1950 | the filename into a temporary buffer first, and then convert it | |
1951 | to POSIX format into the destination buffer. */ | |
1952 | cygwin_buf_t *pathbuf = alloca (exe_name_max_len * sizeof (cygwin_buf_t)); | |
1953 | ||
1954 | len = GetModuleFileNameEx (current_process_handle, | |
1955 | dh_buf, pathbuf, exe_name_max_len); | |
1956 | if (len == 0) | |
1957 | error (_("Error getting executable filename: %u."), | |
1958 | (unsigned) GetLastError ()); | |
1959 | if (cygwin_conv_path (CCP_WIN_W_TO_POSIX, pathbuf, exe_name_ret, | |
1960 | exe_name_max_len) < 0) | |
1961 | error (_("Error converting executable filename to POSIX: %d."), errno); | |
1962 | } | |
1963 | #else | |
1964 | len = GetModuleFileNameEx (current_process_handle, | |
1965 | dh_buf, exe_name_ret, exe_name_max_len); | |
1966 | if (len == 0) | |
1967 | error (_("Error getting executable filename: %u."), | |
1968 | (unsigned) GetLastError ()); | |
1969 | #endif | |
1970 | ||
1971 | return 1; /* success */ | |
1972 | } | |
1973 | ||
1974 | /* The pid_to_exec_file target_ops method for this platform. */ | |
1975 | ||
3ee6f623 | 1976 | static char * |
8dd27370 | 1977 | windows_pid_to_exec_file (struct target_ops *self, int pid) |
47216e51 | 1978 | { |
b3c613f2 | 1979 | static char path[__PMAX]; |
10325bc5 | 1980 | #ifdef __CYGWIN__ |
581e13c1 | 1981 | /* Try to find exe name as symlink target of /proc/<pid>/exe. */ |
33605d39 CF |
1982 | int nchars; |
1983 | char procexe[sizeof ("/proc/4294967295/exe")]; | |
08850b56 PM |
1984 | |
1985 | xsnprintf (procexe, sizeof (procexe), "/proc/%u/exe", pid); | |
33605d39 CF |
1986 | nchars = readlink (procexe, path, sizeof(path)); |
1987 | if (nchars > 0 && nchars < sizeof (path)) | |
47216e51 | 1988 | { |
33605d39 CF |
1989 | path[nchars] = '\0'; /* Got it */ |
1990 | return path; | |
47216e51 | 1991 | } |
10325bc5 PA |
1992 | #endif |
1993 | ||
33605d39 | 1994 | /* If we get here then either Cygwin is hosed, this isn't a Cygwin version |
581e13c1 | 1995 | of gdb, or we're trying to debug a non-Cygwin windows executable. */ |
47f7ffdb | 1996 | if (!windows_get_exec_module_filename (path, sizeof (path))) |
33605d39 CF |
1997 | path[0] = '\0'; |
1998 | ||
1999 | return path; | |
47216e51 CV |
2000 | } |
2001 | ||
24e60978 SC |
2002 | /* Print status information about what we're accessing. */ |
2003 | ||
2004 | static void | |
dc05df57 | 2005 | windows_files_info (struct target_ops *ignore) |
24e60978 | 2006 | { |
181e7f93 PA |
2007 | struct inferior *inf = current_inferior (); |
2008 | ||
24e60978 | 2009 | printf_unfiltered ("\tUsing the running image of %s %s.\n", |
181e7f93 PA |
2010 | inf->attach_flag ? "attached" : "child", |
2011 | target_pid_to_str (inferior_ptid)); | |
24e60978 SC |
2012 | } |
2013 | ||
cd44747c PM |
2014 | /* Modify CreateProcess parameters for use of a new separate console. |
2015 | Parameters are: | |
2016 | *FLAGS: DWORD parameter for general process creation flags. | |
2017 | *SI: STARTUPINFO structure, for which the console window size and | |
2018 | console buffer size is filled in if GDB is running in a console. | |
2019 | to create the new console. | |
2020 | The size of the used font is not available on all versions of | |
2021 | Windows OS. Furthermore, the current font might not be the default | |
2022 | font, but this is still better than before. | |
2023 | If the windows and buffer sizes are computed, | |
2024 | SI->DWFLAGS is changed so that this information is used | |
2025 | by CreateProcess function. */ | |
2026 | ||
2027 | static void | |
2028 | windows_set_console_info (STARTUPINFO *si, DWORD *flags) | |
2029 | { | |
2030 | HANDLE hconsole = CreateFile ("CONOUT$", GENERIC_READ | GENERIC_WRITE, | |
2031 | FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0); | |
2032 | ||
2033 | if (hconsole != INVALID_HANDLE_VALUE) | |
2034 | { | |
2035 | CONSOLE_SCREEN_BUFFER_INFO sbinfo; | |
2036 | COORD font_size; | |
2037 | CONSOLE_FONT_INFO cfi; | |
2038 | ||
2039 | GetCurrentConsoleFont (hconsole, FALSE, &cfi); | |
2040 | font_size = GetConsoleFontSize (hconsole, cfi.nFont); | |
2041 | GetConsoleScreenBufferInfo(hconsole, &sbinfo); | |
2042 | si->dwXSize = sbinfo.srWindow.Right - sbinfo.srWindow.Left + 1; | |
2043 | si->dwYSize = sbinfo.srWindow.Bottom - sbinfo.srWindow.Top + 1; | |
2044 | if (font_size.X) | |
2045 | si->dwXSize *= font_size.X; | |
2046 | else | |
2047 | si->dwXSize *= 8; | |
2048 | if (font_size.Y) | |
2049 | si->dwYSize *= font_size.Y; | |
2050 | else | |
2051 | si->dwYSize *= 12; | |
2052 | si->dwXCountChars = sbinfo.dwSize.X; | |
2053 | si->dwYCountChars = sbinfo.dwSize.Y; | |
2054 | si->dwFlags |= STARTF_USESIZE | STARTF_USECOUNTCHARS; | |
2055 | } | |
2056 | *flags |= CREATE_NEW_CONSOLE; | |
2057 | } | |
2058 | ||
c93dbcba EZ |
2059 | #ifndef __CYGWIN__ |
2060 | /* Function called by qsort to sort environment strings. */ | |
2061 | ||
2062 | static int | |
2063 | envvar_cmp (const void *a, const void *b) | |
2064 | { | |
2065 | const char **p = (const char **) a; | |
2066 | const char **q = (const char **) b; | |
2067 | return strcasecmp (*p, *q); | |
2068 | } | |
2069 | #endif | |
2070 | ||
b7ff339d CV |
2071 | #ifdef __CYGWIN__ |
2072 | static void | |
2073 | clear_win32_environment (char **env) | |
2074 | { | |
2075 | int i; | |
2076 | size_t len; | |
2077 | wchar_t *copy = NULL, *equalpos; | |
2078 | ||
2079 | for (i = 0; env[i] && *env[i]; i++) | |
2080 | { | |
2081 | len = mbstowcs (NULL, env[i], 0) + 1; | |
2082 | copy = (wchar_t *) xrealloc (copy, len * sizeof (wchar_t)); | |
2083 | mbstowcs (copy, env[i], len); | |
2084 | equalpos = wcschr (copy, L'='); | |
2085 | if (equalpos) | |
2086 | *equalpos = L'\0'; | |
2087 | SetEnvironmentVariableW (copy, NULL); | |
2088 | } | |
2089 | xfree (copy); | |
2090 | } | |
2091 | #endif | |
2092 | ||
dc05df57 | 2093 | /* Start an inferior windows child process and sets inferior_ptid to its pid. |
24e60978 SC |
2094 | EXEC_FILE is the file to run. |
2095 | ALLARGS is a string containing the arguments to the program. | |
2096 | ENV is the environment vector to pass. Errors reported with error(). */ | |
2097 | ||
24e60978 | 2098 | static void |
dc05df57 | 2099 | windows_create_inferior (struct target_ops *ops, char *exec_file, |
136d6dae | 2100 | char *allargs, char **in_env, int from_tty) |
24e60978 | 2101 | { |
b3c613f2 | 2102 | STARTUPINFO si; |
41b4aadc | 2103 | #ifdef __CYGWIN__ |
b3c613f2 CF |
2104 | cygwin_buf_t real_path[__PMAX]; |
2105 | cygwin_buf_t shell[__PMAX]; /* Path to shell */ | |
d0d0ab16 | 2106 | const char *sh; |
b3c613f2 CF |
2107 | cygwin_buf_t *toexec; |
2108 | cygwin_buf_t *cygallargs; | |
2109 | cygwin_buf_t *args; | |
b7ff339d CV |
2110 | char **old_env = NULL; |
2111 | PWCHAR w32_env; | |
d0d0ab16 | 2112 | size_t len; |
2becadee CF |
2113 | int tty; |
2114 | int ostdin, ostdout, ostderr; | |
41b4aadc | 2115 | #else |
b3c613f2 CF |
2116 | char real_path[__PMAX]; |
2117 | char shell[__PMAX]; /* Path to shell */ | |
d0d0ab16 CV |
2118 | char *toexec; |
2119 | char *args; | |
8f205f9c | 2120 | size_t args_len; |
41b4aadc | 2121 | HANDLE tty; |
c93dbcba EZ |
2122 | char *w32env; |
2123 | char *temp; | |
2124 | size_t envlen; | |
2125 | int i; | |
2126 | size_t envsize; | |
2127 | char **env; | |
41b4aadc | 2128 | #endif |
d0d0ab16 CV |
2129 | PROCESS_INFORMATION pi; |
2130 | BOOL ret; | |
2131 | DWORD flags = 0; | |
3cb3b8df | 2132 | const char *inferior_io_terminal = get_inferior_io_terminal (); |
24e60978 SC |
2133 | |
2134 | if (!exec_file) | |
8a3fe4f8 | 2135 | error (_("No executable specified, use `target exec'.")); |
24e60978 SC |
2136 | |
2137 | memset (&si, 0, sizeof (si)); | |
2138 | si.cb = sizeof (si); | |
2139 | ||
d0d0ab16 CV |
2140 | if (new_group) |
2141 | flags |= CREATE_NEW_PROCESS_GROUP; | |
2142 | ||
2143 | if (new_console) | |
cd44747c | 2144 | windows_set_console_info (&si, &flags); |
d0d0ab16 | 2145 | |
10325bc5 | 2146 | #ifdef __CYGWIN__ |
349b409f | 2147 | if (!useshell) |
dfe7f3ac | 2148 | { |
d0d0ab16 CV |
2149 | flags |= DEBUG_ONLY_THIS_PROCESS; |
2150 | if (cygwin_conv_path (CCP_POSIX_TO_WIN_W, exec_file, real_path, | |
b3c613f2 | 2151 | __PMAX * sizeof (cygwin_buf_t)) < 0) |
d0d0ab16 | 2152 | error (_("Error starting executable: %d"), errno); |
dfe7f3ac | 2153 | toexec = real_path; |
b3c613f2 | 2154 | #ifdef __USEWIDE |
d0d0ab16 CV |
2155 | len = mbstowcs (NULL, allargs, 0) + 1; |
2156 | if (len == (size_t) -1) | |
2157 | error (_("Error starting executable: %d"), errno); | |
2158 | cygallargs = (wchar_t *) alloca (len * sizeof (wchar_t)); | |
2159 | mbstowcs (cygallargs, allargs, len); | |
60c5c021 CF |
2160 | #else |
2161 | cygallargs = allargs; | |
b3c613f2 | 2162 | #endif |
dfe7f3ac CF |
2163 | } |
2164 | else | |
2165 | { | |
349b409f CF |
2166 | sh = getenv ("SHELL"); |
2167 | if (!sh) | |
2168 | sh = "/bin/sh"; | |
b3c613f2 | 2169 | if (cygwin_conv_path (CCP_POSIX_TO_WIN_W, sh, shell, __PMAX) < 0) |
d0d0ab16 | 2170 | error (_("Error starting executable via shell: %d"), errno); |
b3c613f2 | 2171 | #ifdef __USEWIDE |
d0d0ab16 CV |
2172 | len = sizeof (L" -c 'exec '") + mbstowcs (NULL, exec_file, 0) |
2173 | + mbstowcs (NULL, allargs, 0) + 2; | |
2174 | cygallargs = (wchar_t *) alloca (len * sizeof (wchar_t)); | |
2175 | swprintf (cygallargs, len, L" -c 'exec %s %s'", exec_file, allargs); | |
b3c613f2 | 2176 | #else |
08850b56 PM |
2177 | len = (sizeof (" -c 'exec '") + strlen (exec_file) |
2178 | + strlen (allargs) + 2); | |
2179 | cygallargs = (char *) alloca (len); | |
2180 | xsnprintf (cygallargs, len, " -c 'exec %s %s'", exec_file, allargs); | |
b3c613f2 | 2181 | #endif |
dfe7f3ac | 2182 | toexec = shell; |
d0d0ab16 | 2183 | flags |= DEBUG_PROCESS; |
dfe7f3ac | 2184 | } |
b3c613f2 CF |
2185 | |
2186 | #ifdef __USEWIDE | |
2187 | args = (cygwin_buf_t *) alloca ((wcslen (toexec) + wcslen (cygallargs) + 2) | |
2188 | * sizeof (wchar_t)); | |
d0d0ab16 CV |
2189 | wcscpy (args, toexec); |
2190 | wcscat (args, L" "); | |
2191 | wcscat (args, cygallargs); | |
b3c613f2 CF |
2192 | #else |
2193 | args = (cygwin_buf_t *) alloca (strlen (toexec) + strlen (cygallargs) + 2); | |
2194 | strcpy (args, toexec); | |
2195 | strcat (args, " "); | |
2196 | strcat (args, cygallargs); | |
2197 | #endif | |
2198 | ||
b7ff339d CV |
2199 | #ifdef CW_CVT_ENV_TO_WINENV |
2200 | /* First try to create a direct Win32 copy of the POSIX environment. */ | |
2201 | w32_env = (PWCHAR) cygwin_internal (CW_CVT_ENV_TO_WINENV, in_env); | |
2202 | if (w32_env != (PWCHAR) -1) | |
2203 | flags |= CREATE_UNICODE_ENVIRONMENT; | |
2204 | else | |
2205 | /* If that fails, fall back to old method tweaking GDB's environment. */ | |
2206 | #endif | |
2207 | { | |
2208 | /* Reset all Win32 environment variables to avoid leftover on next run. */ | |
2209 | clear_win32_environment (environ); | |
2210 | /* Prepare the environment vars for CreateProcess. */ | |
2211 | old_env = environ; | |
2212 | environ = in_env; | |
2213 | cygwin_internal (CW_SYNC_WINENV); | |
2214 | w32_env = NULL; | |
2215 | } | |
1750a5ef | 2216 | |
2becadee CF |
2217 | if (!inferior_io_terminal) |
2218 | tty = ostdin = ostdout = ostderr = -1; | |
2219 | else | |
2220 | { | |
2221 | tty = open (inferior_io_terminal, O_RDWR | O_NOCTTY); | |
2222 | if (tty < 0) | |
2223 | { | |
2224 | print_sys_errmsg (inferior_io_terminal, errno); | |
2225 | ostdin = ostdout = ostderr = -1; | |
2226 | } | |
2227 | else | |
2228 | { | |
2229 | ostdin = dup (0); | |
2230 | ostdout = dup (1); | |
2231 | ostderr = dup (2); | |
2232 | dup2 (tty, 0); | |
2233 | dup2 (tty, 1); | |
2234 | dup2 (tty, 2); | |
2235 | } | |
2236 | } | |
d0d0ab16 CV |
2237 | |
2238 | windows_init_thread_list (); | |
b3c613f2 CF |
2239 | ret = CreateProcess (0, |
2240 | args, /* command line */ | |
2241 | NULL, /* Security */ | |
2242 | NULL, /* thread */ | |
2243 | TRUE, /* inherit handles */ | |
2244 | flags, /* start flags */ | |
b7ff339d | 2245 | w32_env, /* environment */ |
b3c613f2 CF |
2246 | NULL, /* current directory */ |
2247 | &si, | |
2248 | &pi); | |
b7ff339d CV |
2249 | if (w32_env) |
2250 | /* Just free the Win32 environment, if it could be created. */ | |
2251 | free (w32_env); | |
2252 | else | |
2253 | { | |
2254 | /* Reset all environment variables to avoid leftover on next run. */ | |
2255 | clear_win32_environment (in_env); | |
2256 | /* Restore normal GDB environment variables. */ | |
2257 | environ = old_env; | |
2258 | cygwin_internal (CW_SYNC_WINENV); | |
2259 | } | |
2260 | ||
d0d0ab16 CV |
2261 | if (tty >= 0) |
2262 | { | |
2263 | close (tty); | |
2264 | dup2 (ostdin, 0); | |
2265 | dup2 (ostdout, 1); | |
2266 | dup2 (ostderr, 2); | |
2267 | close (ostdin); | |
2268 | close (ostdout); | |
2269 | close (ostderr); | |
2270 | } | |
41b4aadc | 2271 | #else |
b3c613f2 | 2272 | toexec = exec_file; |
8f205f9c JB |
2273 | /* Build the command line, a space-separated list of tokens where |
2274 | the first token is the name of the module to be executed. | |
2275 | To avoid ambiguities introduced by spaces in the module name, | |
2276 | we quote it. */ | |
2277 | args_len = strlen (toexec) + 2 /* quotes */ + strlen (allargs) + 2; | |
2278 | args = alloca (args_len); | |
2279 | xsnprintf (args, args_len, "\"%s\" %s", toexec, allargs); | |
d0d0ab16 | 2280 | |
d0d0ab16 CV |
2281 | flags |= DEBUG_ONLY_THIS_PROCESS; |
2282 | ||
41b4aadc CF |
2283 | if (!inferior_io_terminal) |
2284 | tty = INVALID_HANDLE_VALUE; | |
2285 | else | |
2286 | { | |
2287 | SECURITY_ATTRIBUTES sa; | |
2288 | sa.nLength = sizeof(sa); | |
2289 | sa.lpSecurityDescriptor = 0; | |
2290 | sa.bInheritHandle = TRUE; | |
2291 | tty = CreateFileA (inferior_io_terminal, GENERIC_READ | GENERIC_WRITE, | |
2292 | 0, &sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); | |
2293 | if (tty == INVALID_HANDLE_VALUE) | |
2294 | warning (_("Warning: Failed to open TTY %s, error %#x."), | |
2295 | inferior_io_terminal, (unsigned) GetLastError ()); | |
2296 | else | |
2297 | { | |
2298 | si.hStdInput = tty; | |
2299 | si.hStdOutput = tty; | |
2300 | si.hStdError = tty; | |
2301 | si.dwFlags |= STARTF_USESTDHANDLES; | |
2302 | } | |
2303 | } | |
2becadee | 2304 | |
c93dbcba EZ |
2305 | /* CreateProcess takes the environment list as a null terminated set of |
2306 | strings (i.e. two nulls terminate the list). */ | |
2307 | ||
2308 | /* Get total size for env strings. */ | |
2309 | for (envlen = 0, i = 0; in_env[i] && *in_env[i]; i++) | |
2310 | envlen += strlen (in_env[i]) + 1; | |
2311 | ||
2312 | envsize = sizeof (in_env[0]) * (i + 1); | |
2313 | env = (char **) alloca (envsize); | |
2314 | memcpy (env, in_env, envsize); | |
2315 | /* Windows programs expect the environment block to be sorted. */ | |
2316 | qsort (env, i, sizeof (char *), envvar_cmp); | |
2317 | ||
2318 | w32env = alloca (envlen + 1); | |
2319 | ||
2320 | /* Copy env strings into new buffer. */ | |
2321 | for (temp = w32env, i = 0; env[i] && *env[i]; i++) | |
2322 | { | |
2323 | strcpy (temp, env[i]); | |
2324 | temp += strlen (temp) + 1; | |
2325 | } | |
2326 | ||
2327 | /* Final nil string to terminate new env. */ | |
2328 | *temp = 0; | |
2329 | ||
dc05df57 | 2330 | windows_init_thread_list (); |
d0d0ab16 CV |
2331 | ret = CreateProcessA (0, |
2332 | args, /* command line */ | |
2333 | NULL, /* Security */ | |
2334 | NULL, /* thread */ | |
2335 | TRUE, /* inherit handles */ | |
2336 | flags, /* start flags */ | |
c93dbcba | 2337 | w32env, /* environment */ |
d0d0ab16 CV |
2338 | NULL, /* current directory */ |
2339 | &si, | |
2340 | &pi); | |
41b4aadc CF |
2341 | if (tty != INVALID_HANDLE_VALUE) |
2342 | CloseHandle (tty); | |
10325bc5 | 2343 | #endif |
2becadee | 2344 | |
24e60978 | 2345 | if (!ret) |
d50a0ce2 | 2346 | error (_("Error creating process %s, (error %u)."), |
8a3fe4f8 | 2347 | exec_file, (unsigned) GetLastError ()); |
24e60978 | 2348 | |
c1766e7d PM |
2349 | CloseHandle (pi.hThread); |
2350 | CloseHandle (pi.hProcess); | |
2351 | ||
dfe7f3ac CF |
2352 | if (useshell && shell[0] != '\0') |
2353 | saw_create = -1; | |
2354 | else | |
2355 | saw_create = 0; | |
2356 | ||
dc05df57 | 2357 | do_initial_windows_stuff (ops, pi.dwProcessId, 0); |
d3a09475 | 2358 | |
17617f2d | 2359 | /* windows_continue (DBG_CONTINUE, -1, 0); */ |
24e60978 SC |
2360 | } |
2361 | ||
2362 | static void | |
dc05df57 | 2363 | windows_mourn_inferior (struct target_ops *ops) |
24e60978 | 2364 | { |
17617f2d | 2365 | (void) windows_continue (DBG_CONTINUE, -1, 0); |
fa4ba8da | 2366 | i386_cleanup_dregs(); |
bf25528d CF |
2367 | if (open_process_used) |
2368 | { | |
2369 | CHECK (CloseHandle (current_process_handle)); | |
2370 | open_process_used = 0; | |
2371 | } | |
c1ee2fb3 | 2372 | inf_child_mourn_inferior (ops); |
24e60978 SC |
2373 | } |
2374 | ||
24e60978 | 2375 | /* Send a SIGINT to the process group. This acts just like the user typed a |
581e13c1 | 2376 | ^C on the controlling terminal. */ |
24e60978 | 2377 | |
b607efe7 | 2378 | static void |
1eab8a48 | 2379 | windows_stop (struct target_ops *self, ptid_t ptid) |
24e60978 | 2380 | { |
1ef980b9 | 2381 | DEBUG_EVENTS (("gdb: GenerateConsoleCtrlEvent (CTRLC_EVENT, 0)\n")); |
1e37c281 | 2382 | CHECK (GenerateConsoleCtrlEvent (CTRL_C_EVENT, current_event.dwProcessId)); |
3a4b77d8 | 2383 | registers_changed (); /* refresh register state */ |
24e60978 SC |
2384 | } |
2385 | ||
44f38867 PA |
2386 | /* Helper for windows_xfer_partial that handles memory transfers. |
2387 | Arguments are like target_xfer_partial. */ | |
2388 | ||
9b409511 | 2389 | static enum target_xfer_status |
44f38867 | 2390 | windows_xfer_memory (gdb_byte *readbuf, const gdb_byte *writebuf, |
9b409511 | 2391 | ULONGEST memaddr, ULONGEST len, ULONGEST *xfered_len) |
24e60978 | 2392 | { |
5732a500 | 2393 | SIZE_T done = 0; |
44f38867 | 2394 | BOOL success; |
9e52adf9 | 2395 | DWORD lasterror = 0; |
44f38867 PA |
2396 | |
2397 | if (writebuf != NULL) | |
24e60978 | 2398 | { |
a2388568 | 2399 | DEBUG_MEM (("gdb: write target memory, %s bytes at %s\n", |
b55e14c7 | 2400 | pulongest (len), core_addr_to_string (memaddr))); |
44f38867 PA |
2401 | success = WriteProcessMemory (current_process_handle, |
2402 | (LPVOID) (uintptr_t) memaddr, writebuf, | |
2403 | len, &done); | |
9e52adf9 | 2404 | if (!success) |
7126d5c8 | 2405 | lasterror = GetLastError (); |
2b008701 | 2406 | FlushInstructionCache (current_process_handle, |
2c647436 | 2407 | (LPCVOID) (uintptr_t) memaddr, len); |
24e60978 SC |
2408 | } |
2409 | else | |
2410 | { | |
a2388568 | 2411 | DEBUG_MEM (("gdb: read target memory, %s bytes at %s\n", |
b55e14c7 | 2412 | pulongest (len), core_addr_to_string (memaddr))); |
44f38867 PA |
2413 | success = ReadProcessMemory (current_process_handle, |
2414 | (LPCVOID) (uintptr_t) memaddr, readbuf, | |
2415 | len, &done); | |
9e52adf9 | 2416 | if (!success) |
7126d5c8 | 2417 | lasterror = GetLastError (); |
24e60978 | 2418 | } |
9b409511 | 2419 | *xfered_len = (ULONGEST) done; |
9e52adf9 | 2420 | if (!success && lasterror == ERROR_PARTIAL_COPY && done > 0) |
9b409511 | 2421 | return TARGET_XFER_OK; |
9e52adf9 | 2422 | else |
9b409511 | 2423 | return success ? TARGET_XFER_OK : TARGET_XFER_E_IO; |
24e60978 SC |
2424 | } |
2425 | ||
3ee6f623 | 2426 | static void |
7d85a9c0 | 2427 | windows_kill_inferior (struct target_ops *ops) |
24e60978 | 2428 | { |
3cee93ac CF |
2429 | CHECK (TerminateProcess (current_process_handle, 0)); |
2430 | ||
b5edcb45 ILT |
2431 | for (;;) |
2432 | { | |
17617f2d | 2433 | if (!windows_continue (DBG_CONTINUE, -1, 1)) |
b5edcb45 | 2434 | break; |
3cee93ac | 2435 | if (!WaitForDebugEvent (¤t_event, INFINITE)) |
b5edcb45 | 2436 | break; |
3cee93ac | 2437 | if (current_event.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT) |
b5edcb45 ILT |
2438 | break; |
2439 | } | |
2440 | ||
581e13c1 | 2441 | target_mourn_inferior (); /* Or just windows_mourn_inferior? */ |
24e60978 SC |
2442 | } |
2443 | ||
24e60978 | 2444 | static void |
de90e03d | 2445 | windows_close (struct target_ops *self) |
24e60978 | 2446 | { |
dc05df57 | 2447 | DEBUG_EVENTS (("gdb: windows_close, inferior_ptid=%d\n", |
dfd4cc63 | 2448 | ptid_get_pid (inferior_ptid))); |
24e60978 | 2449 | } |
1ef980b9 | 2450 | |
581e13c1 | 2451 | /* Convert pid to printable format. */ |
3ee6f623 | 2452 | static char * |
117de6a9 | 2453 | windows_pid_to_str (struct target_ops *ops, ptid_t ptid) |
24e60978 | 2454 | { |
3ee6f623 | 2455 | static char buf[80]; |
3ee6f623 | 2456 | |
2dc38344 PA |
2457 | if (ptid_get_tid (ptid) != 0) |
2458 | { | |
2459 | snprintf (buf, sizeof (buf), "Thread %d.0x%lx", | |
2460 | ptid_get_pid (ptid), ptid_get_tid (ptid)); | |
2461 | return buf; | |
2462 | } | |
2463 | ||
2464 | return normal_pid_to_str (ptid); | |
3ee6f623 CF |
2465 | } |
2466 | ||
9b409511 | 2467 | static enum target_xfer_status |
dc05df57 | 2468 | windows_xfer_shared_libraries (struct target_ops *ops, |
9b409511 YQ |
2469 | enum target_object object, const char *annex, |
2470 | gdb_byte *readbuf, const gdb_byte *writebuf, | |
2471 | ULONGEST offset, ULONGEST len, | |
2472 | ULONGEST *xfered_len) | |
3cb8e7f6 | 2473 | { |
de1b3c3d PA |
2474 | struct obstack obstack; |
2475 | const char *buf; | |
2476 | LONGEST len_avail; | |
3cb8e7f6 | 2477 | struct so_list *so; |
3cb8e7f6 | 2478 | |
de1b3c3d | 2479 | if (writebuf) |
2ed4b548 | 2480 | return TARGET_XFER_E_IO; |
3cb8e7f6 | 2481 | |
de1b3c3d PA |
2482 | obstack_init (&obstack); |
2483 | obstack_grow_str (&obstack, "<library-list>\n"); | |
2484 | for (so = solib_start.next; so; so = so->next) | |
581e13c1 MS |
2485 | windows_xfer_shared_library (so->so_name, (CORE_ADDR) |
2486 | (uintptr_t) so->lm_info->load_addr, | |
f5656ead | 2487 | target_gdbarch (), &obstack); |
de1b3c3d | 2488 | obstack_grow_str0 (&obstack, "</library-list>\n"); |
3cb8e7f6 | 2489 | |
de1b3c3d PA |
2490 | buf = obstack_finish (&obstack); |
2491 | len_avail = strlen (buf); | |
2492 | if (offset >= len_avail) | |
49dc7f4b PM |
2493 | len= 0; |
2494 | else | |
2495 | { | |
2496 | if (len > len_avail - offset) | |
2497 | len = len_avail - offset; | |
2498 | memcpy (readbuf, buf + offset, len); | |
2499 | } | |
3cb8e7f6 | 2500 | |
de1b3c3d | 2501 | obstack_free (&obstack, NULL); |
9b409511 | 2502 | *xfered_len = (ULONGEST) len; |
0837c976 | 2503 | return len != 0 ? TARGET_XFER_OK : TARGET_XFER_EOF; |
3cb8e7f6 CF |
2504 | } |
2505 | ||
9b409511 | 2506 | static enum target_xfer_status |
dc05df57 | 2507 | windows_xfer_partial (struct target_ops *ops, enum target_object object, |
9b409511 YQ |
2508 | const char *annex, gdb_byte *readbuf, |
2509 | const gdb_byte *writebuf, ULONGEST offset, ULONGEST len, | |
2510 | ULONGEST *xfered_len) | |
3cb8e7f6 | 2511 | { |
de1b3c3d | 2512 | switch (object) |
3cb8e7f6 | 2513 | { |
de1b3c3d | 2514 | case TARGET_OBJECT_MEMORY: |
9b409511 | 2515 | return windows_xfer_memory (readbuf, writebuf, offset, len, xfered_len); |
de1b3c3d PA |
2516 | |
2517 | case TARGET_OBJECT_LIBRARIES: | |
dc05df57 | 2518 | return windows_xfer_shared_libraries (ops, object, annex, readbuf, |
9b409511 | 2519 | writebuf, offset, len, xfered_len); |
3929abe9 | 2520 | |
de1b3c3d PA |
2521 | default: |
2522 | if (ops->beneath != NULL) | |
2523 | return ops->beneath->to_xfer_partial (ops->beneath, object, annex, | |
9b409511 YQ |
2524 | readbuf, writebuf, offset, len, |
2525 | xfered_len); | |
2ed4b548 | 2526 | return TARGET_XFER_E_IO; |
3929abe9 | 2527 | } |
02c5aecd CF |
2528 | } |
2529 | ||
711e434b PM |
2530 | /* Provide thread local base, i.e. Thread Information Block address. |
2531 | Returns 1 if ptid is found and sets *ADDR to thread_local_base. */ | |
2532 | ||
2533 | static int | |
bd7ae0f5 TT |
2534 | windows_get_tib_address (struct target_ops *self, |
2535 | ptid_t ptid, CORE_ADDR *addr) | |
711e434b PM |
2536 | { |
2537 | thread_info *th; | |
2538 | ||
2539 | th = thread_rec (ptid_get_tid (ptid), 0); | |
2540 | if (th == NULL) | |
2541 | return 0; | |
2542 | ||
2543 | if (addr != NULL) | |
2544 | *addr = th->thread_local_base; | |
2545 | ||
2546 | return 1; | |
2547 | } | |
2548 | ||
1e2f1c5c | 2549 | static ptid_t |
1e6b91a4 | 2550 | windows_get_ada_task_ptid (struct target_ops *self, long lwp, long thread) |
1e2f1c5c JB |
2551 | { |
2552 | return ptid_build (ptid_get_pid (inferior_ptid), 0, lwp); | |
2553 | } | |
2554 | ||
51a9c8c5 PA |
2555 | static struct target_ops * |
2556 | windows_target (void) | |
3ee6f623 | 2557 | { |
51a9c8c5 PA |
2558 | struct target_ops *t = inf_child_target (); |
2559 | ||
51a9c8c5 PA |
2560 | t->to_close = windows_close; |
2561 | t->to_attach = windows_attach; | |
2562 | t->to_attach_no_wait = 1; | |
2563 | t->to_detach = windows_detach; | |
2564 | t->to_resume = windows_resume; | |
2565 | t->to_wait = windows_wait; | |
2566 | t->to_fetch_registers = windows_fetch_inferior_registers; | |
2567 | t->to_store_registers = windows_store_inferior_registers; | |
2568 | t->to_xfer_partial = windows_xfer_partial; | |
2569 | t->to_files_info = windows_files_info; | |
2570 | t->to_kill = windows_kill_inferior; | |
2571 | t->to_create_inferior = windows_create_inferior; | |
2572 | t->to_mourn_inferior = windows_mourn_inferior; | |
2573 | t->to_thread_alive = windows_thread_alive; | |
2574 | t->to_pid_to_str = windows_pid_to_str; | |
2575 | t->to_stop = windows_stop; | |
2576 | t->to_pid_to_exec_file = windows_pid_to_exec_file; | |
2577 | t->to_get_ada_task_ptid = windows_get_ada_task_ptid; | |
2578 | t->to_get_tib_address = windows_get_tib_address; | |
2579 | ||
2580 | return t; | |
c719b714 | 2581 | } |
24e60978 | 2582 | |
3929abe9 | 2583 | static void |
dc05df57 | 2584 | set_windows_aliases (char *argv0) |
3929abe9 CF |
2585 | { |
2586 | add_info_alias ("dll", "sharedlibrary", 1); | |
2587 | } | |
2588 | ||
d603d4b3 JK |
2589 | /* -Wmissing-prototypes */ |
2590 | extern initialize_file_ftype _initialize_windows_nat; | |
2591 | ||
24e60978 | 2592 | void |
dc05df57 | 2593 | _initialize_windows_nat (void) |
24e60978 | 2594 | { |
fa58ee11 | 2595 | struct cmd_list_element *c; |
51a9c8c5 PA |
2596 | struct target_ops *t; |
2597 | ||
2598 | t = windows_target (); | |
2599 | ||
2600 | i386_use_watchpoints (t); | |
2601 | ||
2602 | i386_dr_low.set_control = cygwin_set_dr7; | |
2603 | i386_dr_low.set_addr = cygwin_set_dr; | |
2604 | i386_dr_low.get_addr = cygwin_get_dr; | |
2605 | i386_dr_low.get_status = cygwin_get_dr6; | |
2606 | i386_dr_low.get_control = cygwin_get_dr7; | |
2607 | ||
2608 | /* i386_dr_low.debug_register_length field is set by | |
2609 | calling i386_set_debug_register_length function | |
2610 | in processor windows specific native file. */ | |
fa58ee11 | 2611 | |
51a9c8c5 | 2612 | add_target (t); |
1ef980b9 | 2613 | |
d0d0ab16 CV |
2614 | #ifdef __CYGWIN__ |
2615 | cygwin_internal (CW_SET_DOS_FILE_WARNING, 0); | |
2616 | #endif | |
2617 | ||
fa58ee11 | 2618 | c = add_com ("dll-symbols", class_files, dll_symbol_command, |
1bedd215 | 2619 | _("Load dll library symbols from FILE.")); |
5ba2abeb | 2620 | set_cmd_completer (c, filename_completer); |
95060284 | 2621 | deprecate_cmd (c, "sharedlibrary"); |
450005e7 | 2622 | |
95060284 JB |
2623 | c = add_com ("add-shared-symbol-files", class_files, dll_symbol_command, |
2624 | _("Load dll library symbols from FILE.")); | |
2625 | set_cmd_completer (c, filename_completer); | |
2626 | deprecate_cmd (c, "sharedlibrary"); | |
70992597 | 2627 | |
95060284 JB |
2628 | c = add_com ("assf", class_files, dll_symbol_command, |
2629 | _("Load dll library symbols from FILE.")); | |
2630 | set_cmd_completer (c, filename_completer); | |
2631 | deprecate_cmd (c, "sharedlibrary"); | |
70992597 | 2632 | |
10325bc5 | 2633 | #ifdef __CYGWIN__ |
5bf193a2 AC |
2634 | add_setshow_boolean_cmd ("shell", class_support, &useshell, _("\ |
2635 | Set use of shell to start subprocess."), _("\ | |
2636 | Show use of shell to start subprocess."), NULL, | |
2637 | NULL, | |
2638 | NULL, /* FIXME: i18n: */ | |
2639 | &setlist, &showlist); | |
2640 | ||
581e13c1 MS |
2641 | add_setshow_boolean_cmd ("cygwin-exceptions", class_support, |
2642 | &cygwin_exceptions, _("\ | |
09280ddf CF |
2643 | Break when an exception is detected in the Cygwin DLL itself."), _("\ |
2644 | Show whether gdb breaks on exceptions in the Cygwin DLL itself."), NULL, | |
2645 | NULL, | |
2646 | NULL, /* FIXME: i18n: */ | |
2647 | &setlist, &showlist); | |
10325bc5 | 2648 | #endif |
09280ddf | 2649 | |
5bf193a2 AC |
2650 | add_setshow_boolean_cmd ("new-console", class_support, &new_console, _("\ |
2651 | Set creation of new console when creating child process."), _("\ | |
2652 | Show creation of new console when creating child process."), NULL, | |
2653 | NULL, | |
2654 | NULL, /* FIXME: i18n: */ | |
2655 | &setlist, &showlist); | |
2656 | ||
2657 | add_setshow_boolean_cmd ("new-group", class_support, &new_group, _("\ | |
2658 | Set creation of new group when creating child process."), _("\ | |
2659 | Show creation of new group when creating child process."), NULL, | |
2660 | NULL, | |
2661 | NULL, /* FIXME: i18n: */ | |
2662 | &setlist, &showlist); | |
2663 | ||
2664 | add_setshow_boolean_cmd ("debugexec", class_support, &debug_exec, _("\ | |
2665 | Set whether to display execution in child process."), _("\ | |
2666 | Show whether to display execution in child process."), NULL, | |
2667 | NULL, | |
2668 | NULL, /* FIXME: i18n: */ | |
2669 | &setlist, &showlist); | |
2670 | ||
2671 | add_setshow_boolean_cmd ("debugevents", class_support, &debug_events, _("\ | |
2672 | Set whether to display kernel events in child process."), _("\ | |
2673 | Show whether to display kernel events in child process."), NULL, | |
2674 | NULL, | |
2675 | NULL, /* FIXME: i18n: */ | |
2676 | &setlist, &showlist); | |
2677 | ||
2678 | add_setshow_boolean_cmd ("debugmemory", class_support, &debug_memory, _("\ | |
2679 | Set whether to display memory accesses in child process."), _("\ | |
2680 | Show whether to display memory accesses in child process."), NULL, | |
2681 | NULL, | |
2682 | NULL, /* FIXME: i18n: */ | |
2683 | &setlist, &showlist); | |
2684 | ||
2685 | add_setshow_boolean_cmd ("debugexceptions", class_support, | |
2686 | &debug_exceptions, _("\ | |
2687 | Set whether to display kernel exceptions in child process."), _("\ | |
2688 | Show whether to display kernel exceptions in child process."), NULL, | |
2689 | NULL, | |
2690 | NULL, /* FIXME: i18n: */ | |
2691 | &setlist, &showlist); | |
1ef980b9 | 2692 | |
711e434b | 2693 | init_w32_command_list (); |
c1748f97 PM |
2694 | |
2695 | add_cmd ("selector", class_info, display_selectors, | |
1a966eab | 2696 | _("Display selectors infos."), |
c1748f97 | 2697 | &info_w32_cmdlist); |
dc05df57 | 2698 | deprecated_init_ui_hook = set_windows_aliases; |
24e60978 | 2699 | } |
3cee93ac | 2700 | |
fa4ba8da PM |
2701 | /* Hardware watchpoint support, adapted from go32-nat.c code. */ |
2702 | ||
2703 | /* Pass the address ADDR to the inferior in the I'th debug register. | |
2704 | Here we just store the address in dr array, the registers will be | |
dc05df57 | 2705 | actually set up when windows_continue is called. */ |
9bb9e8ad | 2706 | static void |
fa4ba8da PM |
2707 | cygwin_set_dr (int i, CORE_ADDR addr) |
2708 | { | |
2709 | if (i < 0 || i > 3) | |
2710 | internal_error (__FILE__, __LINE__, | |
e2e0b3e5 | 2711 | _("Invalid register %d in cygwin_set_dr.\n"), i); |
41b4aadc | 2712 | dr[i] = addr; |
fa4ba8da PM |
2713 | debug_registers_changed = 1; |
2714 | debug_registers_used = 1; | |
2715 | } | |
2716 | ||
2717 | /* Pass the value VAL to the inferior in the DR7 debug control | |
2718 | register. Here we just store the address in D_REGS, the watchpoint | |
dc05df57 | 2719 | will be actually set up in windows_wait. */ |
9bb9e8ad PM |
2720 | static void |
2721 | cygwin_set_dr7 (unsigned long val) | |
fa4ba8da | 2722 | { |
9bb9e8ad | 2723 | dr[7] = (CORE_ADDR) val; |
fa4ba8da PM |
2724 | debug_registers_changed = 1; |
2725 | debug_registers_used = 1; | |
2726 | } | |
2727 | ||
7b50312a PA |
2728 | /* Get the value of debug register I from the inferior. */ |
2729 | ||
2730 | static CORE_ADDR | |
2731 | cygwin_get_dr (int i) | |
2732 | { | |
2733 | return dr[i]; | |
2734 | } | |
2735 | ||
fa4ba8da PM |
2736 | /* Get the value of the DR6 debug status register from the inferior. |
2737 | Here we just return the value stored in dr[6] | |
2738 | by the last call to thread_rec for current_event.dwThreadId id. */ | |
9bb9e8ad | 2739 | static unsigned long |
fa4ba8da PM |
2740 | cygwin_get_dr6 (void) |
2741 | { | |
9bb9e8ad | 2742 | return (unsigned long) dr[6]; |
fa4ba8da PM |
2743 | } |
2744 | ||
7b50312a PA |
2745 | /* Get the value of the DR7 debug status register from the inferior. |
2746 | Here we just return the value stored in dr[7] by the last call to | |
2747 | thread_rec for current_event.dwThreadId id. */ | |
2748 | ||
2749 | static unsigned long | |
2750 | cygwin_get_dr7 (void) | |
2751 | { | |
2752 | return (unsigned long) dr[7]; | |
2753 | } | |
2754 | ||
2dc38344 | 2755 | /* Determine if the thread referenced by "ptid" is alive |
3cee93ac | 2756 | by "polling" it. If WaitForSingleObject returns WAIT_OBJECT_0 |
581e13c1 | 2757 | it means that the thread has died. Otherwise it is assumed to be alive. */ |
3cee93ac | 2758 | static int |
28439f5e | 2759 | windows_thread_alive (struct target_ops *ops, ptid_t ptid) |
3cee93ac | 2760 | { |
2dc38344 PA |
2761 | int tid; |
2762 | ||
2763 | gdb_assert (ptid_get_tid (ptid) != 0); | |
2764 | tid = ptid_get_tid (ptid); | |
39f77062 | 2765 | |
581e13c1 MS |
2766 | return WaitForSingleObject (thread_rec (tid, FALSE)->h, 0) == WAIT_OBJECT_0 |
2767 | ? FALSE : TRUE; | |
3cee93ac CF |
2768 | } |
2769 | ||
d603d4b3 JK |
2770 | /* -Wmissing-prototypes */ |
2771 | extern initialize_file_ftype _initialize_check_for_gdb_ini; | |
2772 | ||
2a3d5645 CF |
2773 | void |
2774 | _initialize_check_for_gdb_ini (void) | |
2775 | { | |
2776 | char *homedir; | |
2777 | if (inhibit_gdbinit) | |
2778 | return; | |
2779 | ||
2780 | homedir = getenv ("HOME"); | |
2781 | if (homedir) | |
2782 | { | |
2783 | char *p; | |
2784 | char *oldini = (char *) alloca (strlen (homedir) + | |
2785 | sizeof ("/gdb.ini")); | |
2786 | strcpy (oldini, homedir); | |
2787 | p = strchr (oldini, '\0'); | |
0ba1096a | 2788 | if (p > oldini && !IS_DIR_SEPARATOR (p[-1])) |
2a3d5645 CF |
2789 | *p++ = '/'; |
2790 | strcpy (p, "gdb.ini"); | |
2791 | if (access (oldini, 0) == 0) | |
2792 | { | |
2793 | int len = strlen (oldini); | |
2794 | char *newini = alloca (len + 1); | |
08850b56 PM |
2795 | |
2796 | xsnprintf (newini, len + 1, "%.*s.gdbinit", | |
2797 | (int) (len - (sizeof ("gdb.ini") - 1)), oldini); | |
8a3fe4f8 | 2798 | warning (_("obsolete '%s' found. Rename to '%s'."), oldini, newini); |
2a3d5645 CF |
2799 | } |
2800 | } | |
2801 | } | |
33605d39 | 2802 | |
2b008701 | 2803 | /* Define dummy functions which always return error for the rare cases where |
581e13c1 | 2804 | these functions could not be found. */ |
2b008701 CF |
2805 | static BOOL WINAPI |
2806 | bad_DebugActiveProcessStop (DWORD w) | |
2807 | { | |
2808 | return FALSE; | |
2809 | } | |
2810 | static BOOL WINAPI | |
2811 | bad_DebugBreakProcess (HANDLE w) | |
2812 | { | |
2813 | return FALSE; | |
2814 | } | |
2815 | static BOOL WINAPI | |
2816 | bad_DebugSetProcessKillOnExit (BOOL w) | |
2817 | { | |
2818 | return FALSE; | |
2819 | } | |
2820 | static BOOL WINAPI | |
2821 | bad_EnumProcessModules (HANDLE w, HMODULE *x, DWORD y, LPDWORD z) | |
2822 | { | |
2823 | return FALSE; | |
2824 | } | |
b3c613f2 CF |
2825 | |
2826 | #ifdef __USEWIDE | |
2b008701 | 2827 | static DWORD WINAPI |
b3c613f2 | 2828 | bad_GetModuleFileNameExW (HANDLE w, HMODULE x, LPWSTR y, DWORD z) |
2b008701 CF |
2829 | { |
2830 | return 0; | |
2831 | } | |
d0d0ab16 CV |
2832 | #else |
2833 | static DWORD WINAPI | |
b3c613f2 | 2834 | bad_GetModuleFileNameExA (HANDLE w, HMODULE x, LPSTR y, DWORD z) |
d0d0ab16 CV |
2835 | { |
2836 | return 0; | |
2837 | } | |
2838 | #endif | |
b3c613f2 | 2839 | |
2b008701 CF |
2840 | static BOOL WINAPI |
2841 | bad_GetModuleInformation (HANDLE w, HMODULE x, LPMODULEINFO y, DWORD z) | |
2842 | { | |
2843 | return FALSE; | |
2844 | } | |
2845 | ||
418c6cb3 CF |
2846 | static BOOL WINAPI |
2847 | bad_OpenProcessToken (HANDLE w, DWORD x, PHANDLE y) | |
2848 | { | |
2849 | return FALSE; | |
2850 | } | |
2851 | ||
cd44747c PM |
2852 | static BOOL WINAPI |
2853 | bad_GetCurrentConsoleFont (HANDLE w, BOOL bMaxWindow, CONSOLE_FONT_INFO *f) | |
2854 | { | |
2855 | f->nFont = 0; | |
2856 | return 1; | |
2857 | } | |
2858 | static COORD WINAPI | |
2859 | bad_GetConsoleFontSize (HANDLE w, DWORD nFont) | |
2860 | { | |
2861 | COORD size; | |
2862 | size.X = 8; | |
2863 | size.Y = 12; | |
2864 | return size; | |
2865 | } | |
2866 | ||
d603d4b3 JK |
2867 | /* -Wmissing-prototypes */ |
2868 | extern initialize_file_ftype _initialize_loadable; | |
2869 | ||
2b008701 | 2870 | /* Load any functions which may not be available in ancient versions |
581e13c1 | 2871 | of Windows. */ |
d603d4b3 | 2872 | |
33605d39 | 2873 | void |
2b008701 | 2874 | _initialize_loadable (void) |
33605d39 | 2875 | { |
2b008701 CF |
2876 | HMODULE hm = NULL; |
2877 | ||
2878 | hm = LoadLibrary ("kernel32.dll"); | |
2879 | if (hm) | |
33605d39 | 2880 | { |
b3c613f2 | 2881 | DebugActiveProcessStop = (void *) |
2b008701 | 2882 | GetProcAddress (hm, "DebugActiveProcessStop"); |
b3c613f2 | 2883 | DebugBreakProcess = (void *) |
2b008701 | 2884 | GetProcAddress (hm, "DebugBreakProcess"); |
b3c613f2 | 2885 | DebugSetProcessKillOnExit = (void *) |
2b008701 | 2886 | GetProcAddress (hm, "DebugSetProcessKillOnExit"); |
cd44747c PM |
2887 | GetConsoleFontSize = (void *) |
2888 | GetProcAddress (hm, "GetConsoleFontSize"); | |
2889 | GetCurrentConsoleFont = (void *) | |
2890 | GetProcAddress (hm, "GetCurrentConsoleFont"); | |
2b008701 | 2891 | } |
33605d39 | 2892 | |
2b008701 | 2893 | /* Set variables to dummy versions of these processes if the function |
581e13c1 | 2894 | wasn't found in kernel32.dll. */ |
b3c613f2 CF |
2895 | if (!DebugBreakProcess) |
2896 | DebugBreakProcess = bad_DebugBreakProcess; | |
2897 | if (!DebugActiveProcessStop || !DebugSetProcessKillOnExit) | |
2b008701 | 2898 | { |
b3c613f2 CF |
2899 | DebugActiveProcessStop = bad_DebugActiveProcessStop; |
2900 | DebugSetProcessKillOnExit = bad_DebugSetProcessKillOnExit; | |
2b008701 | 2901 | } |
cd44747c PM |
2902 | if (!GetConsoleFontSize) |
2903 | GetConsoleFontSize = bad_GetConsoleFontSize; | |
2904 | if (!GetCurrentConsoleFont) | |
2905 | GetCurrentConsoleFont = bad_GetCurrentConsoleFont; | |
33605d39 | 2906 | |
2b008701 | 2907 | /* Load optional functions used for retrieving filename information |
581e13c1 | 2908 | associated with the currently debugged process or its dlls. */ |
2b008701 CF |
2909 | hm = LoadLibrary ("psapi.dll"); |
2910 | if (hm) | |
2911 | { | |
b3c613f2 | 2912 | EnumProcessModules = (void *) |
2b008701 | 2913 | GetProcAddress (hm, "EnumProcessModules"); |
b3c613f2 | 2914 | GetModuleInformation = (void *) |
2b008701 | 2915 | GetProcAddress (hm, "GetModuleInformation"); |
b3c613f2 CF |
2916 | GetModuleFileNameEx = (void *) |
2917 | GetProcAddress (hm, GetModuleFileNameEx_name); | |
33605d39 CF |
2918 | } |
2919 | ||
b3c613f2 | 2920 | if (!EnumProcessModules || !GetModuleInformation || !GetModuleFileNameEx) |
2b008701 CF |
2921 | { |
2922 | /* Set variables to dummy versions of these processes if the function | |
581e13c1 | 2923 | wasn't found in psapi.dll. */ |
b3c613f2 CF |
2924 | EnumProcessModules = bad_EnumProcessModules; |
2925 | GetModuleInformation = bad_GetModuleInformation; | |
2926 | GetModuleFileNameEx = bad_GetModuleFileNameEx; | |
581e13c1 MS |
2927 | /* This will probably fail on Windows 9x/Me. Let the user know |
2928 | that we're missing some functionality. */ | |
2929 | warning(_("\ | |
2930 | cannot automatically find executable file or library to read symbols.\n\ | |
2931 | Use \"file\" or \"dll\" command to load executable/libraries directly.")); | |
418c6cb3 CF |
2932 | } |
2933 | ||
2934 | hm = LoadLibrary ("advapi32.dll"); | |
2935 | if (hm) | |
2936 | { | |
b3c613f2 CF |
2937 | OpenProcessToken = (void *) GetProcAddress (hm, "OpenProcessToken"); |
2938 | LookupPrivilegeValueA = (void *) | |
418c6cb3 | 2939 | GetProcAddress (hm, "LookupPrivilegeValueA"); |
b3c613f2 | 2940 | AdjustTokenPrivileges = (void *) |
418c6cb3 CF |
2941 | GetProcAddress (hm, "AdjustTokenPrivileges"); |
2942 | /* Only need to set one of these since if OpenProcessToken fails nothing | |
581e13c1 MS |
2943 | else is needed. */ |
2944 | if (!OpenProcessToken || !LookupPrivilegeValueA | |
2945 | || !AdjustTokenPrivileges) | |
b3c613f2 | 2946 | OpenProcessToken = bad_OpenProcessToken; |
2b008701 | 2947 | } |
33605d39 | 2948 | } |