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