]>
Commit | Line | Data |
---|---|---|
dc05df57 | 1 | /* Target-vector operations for controlling windows child processes, for GDB. |
0a65a603 | 2 | |
281b533b | 3 | Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, |
4c38e0a4 | 4 | 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
0a65a603 | 5 | |
e6433c28 | 6 | Contributed by Cygnus Solutions, A Red Hat Company. |
e88c49c3 | 7 | |
24e60978 SC |
8 | This file is part of GDB. |
9 | ||
10 | This program is free software; you can redistribute it and/or modify | |
11 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 12 | the Free Software Foundation; either version 3 of the License, or |
24e60978 SC |
13 | (at your option) any later version. |
14 | ||
15 | This program is distributed in the hope that it will be useful, | |
a9762ec7 | 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
24e60978 SC |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | GNU General Public License for more details. | |
19 | ||
20 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 21 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
24e60978 | 22 | |
dfe7f3ac | 23 | /* Originally by Steve Chamberlain, sac@cygnus.com */ |
24e60978 SC |
24 | |
25 | #include "defs.h" | |
26 | #include "frame.h" /* required by inferior.h */ | |
27 | #include "inferior.h" | |
28 | #include "target.h" | |
60250e8b | 29 | #include "exceptions.h" |
24e60978 SC |
30 | #include "gdbcore.h" |
31 | #include "command.h" | |
fa58ee11 | 32 | #include "completer.h" |
4e052eda | 33 | #include "regcache.h" |
2a3d5645 | 34 | #include "top.h" |
403d9909 CF |
35 | #include <signal.h> |
36 | #include <sys/types.h> | |
37 | #include <fcntl.h> | |
38 | #include <stdlib.h> | |
39 | #include <windows.h> | |
40 | #include <imagehlp.h> | |
2b008701 | 41 | #include <psapi.h> |
10325bc5 | 42 | #ifdef __CYGWIN__ |
403d9909 | 43 | #include <sys/cygwin.h> |
10325bc5 | 44 | #endif |
a244bdca | 45 | #include <signal.h> |
cad9cd60 | 46 | |
24e60978 | 47 | #include "buildsym.h" |
1ef980b9 SC |
48 | #include "symfile.h" |
49 | #include "objfiles.h" | |
de1b3c3d | 50 | #include "gdb_obstack.h" |
24e60978 | 51 | #include "gdb_string.h" |
fdfa3315 | 52 | #include "gdbthread.h" |
24e60978 | 53 | #include "gdbcmd.h" |
1750a5ef | 54 | #include <sys/param.h> |
1e37c281 | 55 | #include <unistd.h> |
4646aa9d | 56 | #include "exec.h" |
3ee6f623 | 57 | #include "solist.h" |
3cb8e7f6 | 58 | #include "solib.h" |
de1b3c3d | 59 | #include "xml-support.h" |
24e60978 | 60 | |
6c7de422 MK |
61 | #include "i386-tdep.h" |
62 | #include "i387-tdep.h" | |
63 | ||
31b060a2 CF |
64 | #include "windows-tdep.h" |
65 | #include "windows-nat.h" | |
9bb9e8ad | 66 | #include "i386-nat.h" |
ecc13e53 | 67 | #include "complaints.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 | |
74 | #define GetModuleFileNameExA dyn_GetModuleFileNameExA | |
75 | #define GetModuleInformation dyn_GetModuleInformation | |
418c6cb3 CF |
76 | #define LookupPrivilegeValueA dyn_LookupPrivilegeValueA |
77 | #define OpenProcessToken dyn_OpenProcessToken | |
2b008701 | 78 | |
418c6cb3 CF |
79 | static BOOL WINAPI (*AdjustTokenPrivileges)(HANDLE, BOOL, PTOKEN_PRIVILEGES, |
80 | DWORD, PTOKEN_PRIVILEGES, PDWORD); | |
2b008701 CF |
81 | static BOOL WINAPI (*DebugActiveProcessStop) (DWORD); |
82 | static BOOL WINAPI (*DebugBreakProcess) (HANDLE); | |
83 | static BOOL WINAPI (*DebugSetProcessKillOnExit) (BOOL); | |
84 | static BOOL WINAPI (*EnumProcessModules) (HANDLE, HMODULE *, DWORD, | |
85 | LPDWORD); | |
86 | static DWORD WINAPI (*GetModuleFileNameExA) (HANDLE, HMODULE, LPSTR, | |
87 | DWORD); | |
88 | static BOOL WINAPI (*GetModuleInformation) (HANDLE, HMODULE, LPMODULEINFO, | |
89 | DWORD); | |
418c6cb3 CF |
90 | static BOOL WINAPI (*LookupPrivilegeValueA)(LPCSTR, LPCSTR, PLUID); |
91 | static BOOL WINAPI (*OpenProcessToken)(HANDLE, DWORD, PHANDLE); | |
2b008701 | 92 | |
dc05df57 | 93 | static struct target_ops windows_ops; |
3ee6f623 | 94 | |
10325bc5 | 95 | #ifdef __CYGWIN__ |
a244bdca | 96 | /* The starting and ending address of the cygwin1.dll text segment. */ |
dc05df57 CF |
97 | static CORE_ADDR cygwin_load_start; |
98 | static CORE_ADDR cygwin_load_end; | |
10325bc5 | 99 | #endif |
a244bdca CF |
100 | |
101 | static int have_saved_context; /* True if we've saved context from a cygwin signal. */ | |
102 | static CONTEXT saved_context; /* Containes the saved context from a cygwin signal. */ | |
103 | ||
0714f9bf SS |
104 | /* If we're not using the old Cygwin header file set, define the |
105 | following which never should have been in the generic Win32 API | |
106 | headers in the first place since they were our own invention... */ | |
107 | #ifndef _GNU_H_WINDOWS_H | |
9d3789f7 | 108 | enum |
8e860359 CF |
109 | { |
110 | FLAG_TRACE_BIT = 0x100, | |
111 | CONTEXT_DEBUGGER = (CONTEXT_FULL | CONTEXT_FLOATING_POINT) | |
112 | }; | |
0714f9bf SS |
113 | #endif |
114 | ||
5851ab76 JB |
115 | #ifndef CONTEXT_EXTENDED_REGISTERS |
116 | /* This macro is only defined on ia32. It only makes sense on this target, | |
117 | so define it as zero if not already defined. */ | |
118 | #define CONTEXT_EXTENDED_REGISTERS 0 | |
119 | #endif | |
120 | ||
fa4ba8da PM |
121 | #define CONTEXT_DEBUGGER_DR CONTEXT_DEBUGGER | CONTEXT_DEBUG_REGISTERS \ |
122 | | CONTEXT_EXTENDED_REGISTERS | |
97da3b20 | 123 | |
41b4aadc | 124 | static uintptr_t dr[8]; |
87a45c96 CF |
125 | static int debug_registers_changed; |
126 | static int debug_registers_used; | |
16d905e2 CF |
127 | |
128 | static int windows_initialization_done; | |
6537bb24 | 129 | #define DR6_CLEAR_VALUE 0xffff0ff0 |
97da3b20 | 130 | |
3cee93ac CF |
131 | /* The string sent by cygwin when it processes a signal. |
132 | FIXME: This should be in a cygwin include file. */ | |
3929abe9 CF |
133 | #ifndef _CYGWIN_SIGNAL_STRING |
134 | #define _CYGWIN_SIGNAL_STRING "cYgSiGw00f" | |
135 | #endif | |
3cee93ac | 136 | |
29fe111d | 137 | #define CHECK(x) check (x, __FILE__,__LINE__) |
dfe7f3ac | 138 | #define DEBUG_EXEC(x) if (debug_exec) printf_unfiltered x |
4e52d31c PM |
139 | #define DEBUG_EVENTS(x) if (debug_events) printf_unfiltered x |
140 | #define DEBUG_MEM(x) if (debug_memory) printf_unfiltered x | |
141 | #define DEBUG_EXCEPT(x) if (debug_exceptions) printf_unfiltered x | |
24e60978 | 142 | |
dc05df57 | 143 | static void windows_stop (ptid_t); |
02529b48 | 144 | static int windows_thread_alive (struct target_ops *, ptid_t); |
7d85a9c0 | 145 | static void windows_kill_inferior (struct target_ops *); |
3cee93ac | 146 | |
9bb9e8ad PM |
147 | static void cygwin_set_dr (int i, CORE_ADDR addr); |
148 | static void cygwin_set_dr7 (unsigned long val); | |
149 | static unsigned long cygwin_get_dr6 (void); | |
150 | ||
7393af7c PM |
151 | static enum target_signal last_sig = TARGET_SIGNAL_0; |
152 | /* Set if a signal was received from the debugged process */ | |
153 | ||
3cee93ac | 154 | /* Thread information structure used to track information that is |
6537bb24 | 155 | not available in gdb's thread structure. */ |
3cee93ac | 156 | typedef struct thread_info_struct |
3a4b77d8 JM |
157 | { |
158 | struct thread_info_struct *next; | |
159 | DWORD id; | |
160 | HANDLE h; | |
161 | char *name; | |
6537bb24 | 162 | int suspended; |
3ade5333 | 163 | int reload_context; |
3a4b77d8 | 164 | CONTEXT context; |
1e37c281 | 165 | STACKFRAME sf; |
8e860359 CF |
166 | } |
167 | thread_info; | |
1e37c281 | 168 | |
29fe111d | 169 | static thread_info thread_head; |
24e60978 | 170 | |
24e60978 SC |
171 | /* The process and thread handles for the above context. */ |
172 | ||
3cee93ac CF |
173 | static DEBUG_EVENT current_event; /* The current debug event from |
174 | WaitForDebugEvent */ | |
175 | static HANDLE current_process_handle; /* Currently executing process */ | |
176 | static thread_info *current_thread; /* Info on currently selected thread */ | |
349b409f | 177 | static DWORD main_thread_id; /* Thread ID of the main thread */ |
24e60978 SC |
178 | |
179 | /* Counts of things. */ | |
180 | static int exception_count = 0; | |
181 | static int event_count = 0; | |
dfe7f3ac | 182 | static int saw_create; |
bf25528d | 183 | static int open_process_used = 0; |
24e60978 SC |
184 | |
185 | /* User options. */ | |
186 | static int new_console = 0; | |
10325bc5 | 187 | #ifdef __CYGWIN__ |
09280ddf | 188 | static int cygwin_exceptions = 0; |
10325bc5 | 189 | #endif |
1e37c281 | 190 | static int new_group = 1; |
dfe7f3ac CF |
191 | static int debug_exec = 0; /* show execution */ |
192 | static int debug_events = 0; /* show events from kernel */ | |
193 | static int debug_memory = 0; /* show target memory accesses */ | |
1ef980b9 | 194 | static int debug_exceptions = 0; /* show target exceptions */ |
dfe7f3ac CF |
195 | static int useshell = 0; /* use shell for subprocesses */ |
196 | ||
7e63b4e4 | 197 | /* This vector maps GDB's idea of a register's number into an offset |
dc05df57 | 198 | in the windows exception context vector. |
24e60978 | 199 | |
3cee93ac | 200 | It also contains the bit mask needed to load the register in question. |
24e60978 | 201 | |
7e63b4e4 JB |
202 | The contents of this table can only be computed by the units |
203 | that provide CPU-specific support for Windows native debugging. | |
204 | These units should set the table by calling | |
dc05df57 | 205 | windows_set_context_register_offsets. |
7e63b4e4 | 206 | |
24e60978 SC |
207 | One day we could read a reg, we could inspect the context we |
208 | already have loaded, if it doesn't have the bit set that we need, | |
209 | we read that set of registers in using GetThreadContext. If the | |
210 | context already contains what we need, we just unpack it. Then to | |
211 | write a register, first we have to ensure that the context contains | |
212 | the other regs of the group, and then we copy the info in and set | |
213 | out bit. */ | |
214 | ||
7e63b4e4 | 215 | static const int *mappings; |
d3a09475 | 216 | |
24e60978 SC |
217 | /* This vector maps the target's idea of an exception (extracted |
218 | from the DEBUG_EVENT structure) to GDB's idea. */ | |
219 | ||
220 | struct xlate_exception | |
221 | { | |
222 | int them; | |
223 | enum target_signal us; | |
224 | }; | |
225 | ||
24e60978 SC |
226 | static const struct xlate_exception |
227 | xlate[] = | |
228 | { | |
229 | {EXCEPTION_ACCESS_VIOLATION, TARGET_SIGNAL_SEGV}, | |
9cbf6c0e | 230 | {STATUS_STACK_OVERFLOW, TARGET_SIGNAL_SEGV}, |
24e60978 SC |
231 | {EXCEPTION_BREAKPOINT, TARGET_SIGNAL_TRAP}, |
232 | {DBG_CONTROL_C, TARGET_SIGNAL_INT}, | |
233 | {EXCEPTION_SINGLE_STEP, TARGET_SIGNAL_TRAP}, | |
7393af7c | 234 | {STATUS_FLOAT_DIVIDE_BY_ZERO, TARGET_SIGNAL_FPE}, |
24e60978 SC |
235 | {-1, -1}}; |
236 | ||
7e63b4e4 JB |
237 | /* Set the MAPPINGS static global to OFFSETS. |
238 | See the description of MAPPINGS for more details. */ | |
239 | ||
240 | void | |
dc05df57 | 241 | windows_set_context_register_offsets (const int *offsets) |
7e63b4e4 JB |
242 | { |
243 | mappings = offsets; | |
244 | } | |
245 | ||
fa4ba8da PM |
246 | static void |
247 | check (BOOL ok, const char *file, int line) | |
248 | { | |
249 | if (!ok) | |
dfe7f3ac | 250 | printf_filtered ("error return %s:%d was %lu\n", file, line, |
fa4ba8da PM |
251 | GetLastError ()); |
252 | } | |
253 | ||
6537bb24 PA |
254 | /* Find a thread record given a thread id. If GET_CONTEXT is not 0, |
255 | then also retrieve the context for this thread. If GET_CONTEXT is | |
256 | negative, then don't suspend the thread. */ | |
3cee93ac CF |
257 | static thread_info * |
258 | thread_rec (DWORD id, int get_context) | |
24e60978 | 259 | { |
3cee93ac CF |
260 | thread_info *th; |
261 | ||
3a4b77d8 | 262 | for (th = &thread_head; (th = th->next) != NULL;) |
3cee93ac CF |
263 | if (th->id == id) |
264 | { | |
6537bb24 | 265 | if (!th->suspended && get_context) |
3cee93ac | 266 | { |
8a892701 | 267 | if (get_context > 0 && id != current_event.dwThreadId) |
6537bb24 PA |
268 | { |
269 | if (SuspendThread (th->h) == (DWORD) -1) | |
270 | { | |
271 | DWORD err = GetLastError (); | |
272 | warning (_("SuspendThread failed. (winerr %d)"), | |
273 | (int) err); | |
274 | return NULL; | |
275 | } | |
276 | th->suspended = 1; | |
277 | } | |
3cee93ac | 278 | else if (get_context < 0) |
6537bb24 | 279 | th->suspended = -1; |
3ade5333 | 280 | th->reload_context = 1; |
3cee93ac CF |
281 | } |
282 | return th; | |
283 | } | |
284 | ||
285 | return NULL; | |
286 | } | |
287 | ||
2dc38344 | 288 | /* Add a thread to the thread list. */ |
3cee93ac | 289 | static thread_info * |
dc05df57 | 290 | windows_add_thread (ptid_t ptid, HANDLE h) |
3cee93ac CF |
291 | { |
292 | thread_info *th; | |
2dc38344 PA |
293 | DWORD id; |
294 | ||
295 | gdb_assert (ptid_get_tid (ptid) != 0); | |
296 | ||
297 | id = ptid_get_tid (ptid); | |
3cee93ac CF |
298 | |
299 | if ((th = thread_rec (id, FALSE))) | |
300 | return th; | |
301 | ||
3929abe9 | 302 | th = XZALLOC (thread_info); |
3cee93ac CF |
303 | th->id = id; |
304 | th->h = h; | |
305 | th->next = thread_head.next; | |
306 | thread_head.next = th; | |
2dc38344 PA |
307 | add_thread (ptid); |
308 | /* Set the debug registers for the new thread if they are used. */ | |
fa4ba8da PM |
309 | if (debug_registers_used) |
310 | { | |
311 | /* Only change the value of the debug registers. */ | |
312 | th->context.ContextFlags = CONTEXT_DEBUG_REGISTERS; | |
313 | CHECK (GetThreadContext (th->h, &th->context)); | |
314 | th->context.Dr0 = dr[0]; | |
315 | th->context.Dr1 = dr[1]; | |
316 | th->context.Dr2 = dr[2]; | |
317 | th->context.Dr3 = dr[3]; | |
6537bb24 | 318 | th->context.Dr6 = DR6_CLEAR_VALUE; |
fa4ba8da PM |
319 | th->context.Dr7 = dr[7]; |
320 | CHECK (SetThreadContext (th->h, &th->context)); | |
321 | th->context.ContextFlags = 0; | |
322 | } | |
3cee93ac | 323 | return th; |
24e60978 SC |
324 | } |
325 | ||
3cee93ac CF |
326 | /* Clear out any old thread list and reintialize it to a |
327 | pristine state. */ | |
24e60978 | 328 | static void |
dc05df57 | 329 | windows_init_thread_list (void) |
24e60978 | 330 | { |
3cee93ac CF |
331 | thread_info *th = &thread_head; |
332 | ||
dc05df57 | 333 | DEBUG_EVENTS (("gdb: windows_init_thread_list\n")); |
3cee93ac CF |
334 | init_thread_list (); |
335 | while (th->next != NULL) | |
24e60978 | 336 | { |
3cee93ac CF |
337 | thread_info *here = th->next; |
338 | th->next = here->next; | |
b8c9b27d | 339 | xfree (here); |
24e60978 | 340 | } |
059198c1 | 341 | thread_head.next = NULL; |
3cee93ac CF |
342 | } |
343 | ||
344 | /* Delete a thread from the list of threads */ | |
345 | static void | |
dc05df57 | 346 | windows_delete_thread (ptid_t ptid) |
3cee93ac CF |
347 | { |
348 | thread_info *th; | |
2dc38344 PA |
349 | DWORD id; |
350 | ||
351 | gdb_assert (ptid_get_tid (ptid) != 0); | |
352 | ||
353 | id = ptid_get_tid (ptid); | |
3cee93ac CF |
354 | |
355 | if (info_verbose) | |
2dc38344 PA |
356 | printf_unfiltered ("[Deleting %s]\n", target_pid_to_str (ptid)); |
357 | delete_thread (ptid); | |
3cee93ac CF |
358 | |
359 | for (th = &thread_head; | |
360 | th->next != NULL && th->next->id != id; | |
361 | th = th->next) | |
362 | continue; | |
363 | ||
364 | if (th->next != NULL) | |
24e60978 | 365 | { |
3cee93ac CF |
366 | thread_info *here = th->next; |
367 | th->next = here->next; | |
b8c9b27d | 368 | xfree (here); |
24e60978 SC |
369 | } |
370 | } | |
371 | ||
3cee93ac | 372 | static void |
dc05df57 | 373 | do_windows_fetch_inferior_registers (struct regcache *regcache, int r) |
24e60978 | 374 | { |
1e37c281 | 375 | char *context_offset = ((char *) ¤t_thread->context) + mappings[r]; |
20a6ec49 MD |
376 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
377 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); | |
1e37c281 | 378 | long l; |
6c7de422 | 379 | |
3ade5333 | 380 | if (!current_thread) |
d6dc8049 CF |
381 | return; /* Windows sometimes uses a non-existent thread id in its |
382 | events */ | |
3ade5333 CF |
383 | |
384 | if (current_thread->reload_context) | |
385 | { | |
cb832706 | 386 | #ifdef __COPY_CONTEXT_SIZE |
a244bdca CF |
387 | if (have_saved_context) |
388 | { | |
389 | /* Lie about where the program actually is stopped since cygwin has informed us that | |
390 | we should consider the signal to have occurred at another location which is stored | |
391 | in "saved_context. */ | |
392 | memcpy (¤t_thread->context, &saved_context, __COPY_CONTEXT_SIZE); | |
393 | have_saved_context = 0; | |
394 | } | |
395 | else | |
cb832706 | 396 | #endif |
a244bdca CF |
397 | { |
398 | thread_info *th = current_thread; | |
399 | th->context.ContextFlags = CONTEXT_DEBUGGER_DR; | |
400 | GetThreadContext (th->h, &th->context); | |
2b008701 | 401 | /* Copy dr values from that thread. |
88616312 PM |
402 | But only if there were not modified since last stop. PR gdb/2388 */ |
403 | if (!debug_registers_changed) | |
404 | { | |
405 | dr[0] = th->context.Dr0; | |
406 | dr[1] = th->context.Dr1; | |
407 | dr[2] = th->context.Dr2; | |
408 | dr[3] = th->context.Dr3; | |
409 | dr[6] = th->context.Dr6; | |
410 | dr[7] = th->context.Dr7; | |
411 | } | |
a244bdca | 412 | } |
3ade5333 CF |
413 | current_thread->reload_context = 0; |
414 | } | |
415 | ||
20a6ec49 | 416 | if (r == I387_FISEG_REGNUM (tdep)) |
1e37c281 | 417 | { |
8e860359 | 418 | l = *((long *) context_offset) & 0xffff; |
56be3814 | 419 | regcache_raw_supply (regcache, r, (char *) &l); |
1e37c281 | 420 | } |
20a6ec49 | 421 | else if (r == I387_FOP_REGNUM (tdep)) |
1e37c281 | 422 | { |
8e860359 | 423 | l = (*((long *) context_offset) >> 16) & ((1 << 11) - 1); |
56be3814 | 424 | regcache_raw_supply (regcache, r, (char *) &l); |
1e37c281 JM |
425 | } |
426 | else if (r >= 0) | |
56be3814 | 427 | regcache_raw_supply (regcache, r, context_offset); |
3cee93ac | 428 | else |
24e60978 | 429 | { |
20a6ec49 | 430 | for (r = 0; r < gdbarch_num_regs (gdbarch); r++) |
dc05df57 | 431 | do_windows_fetch_inferior_registers (regcache, r); |
24e60978 | 432 | } |
3cee93ac CF |
433 | } |
434 | ||
435 | static void | |
28439f5e PA |
436 | windows_fetch_inferior_registers (struct target_ops *ops, |
437 | struct regcache *regcache, int r) | |
3cee93ac | 438 | { |
2dc38344 | 439 | current_thread = thread_rec (ptid_get_tid (inferior_ptid), TRUE); |
d6dc8049 CF |
440 | /* Check if current_thread exists. Windows sometimes uses a non-existent |
441 | thread id in its events */ | |
3ade5333 | 442 | if (current_thread) |
dc05df57 | 443 | do_windows_fetch_inferior_registers (regcache, r); |
3cee93ac CF |
444 | } |
445 | ||
446 | static void | |
dc05df57 | 447 | do_windows_store_inferior_registers (const struct regcache *regcache, int r) |
3cee93ac | 448 | { |
3ade5333 | 449 | if (!current_thread) |
d6dc8049 | 450 | /* Windows sometimes uses a non-existent thread id in its events */; |
3ade5333 | 451 | else if (r >= 0) |
56be3814 | 452 | regcache_raw_collect (regcache, r, |
822c9732 | 453 | ((char *) ¤t_thread->context) + mappings[r]); |
24e60978 SC |
454 | else |
455 | { | |
40a6adc1 | 456 | for (r = 0; r < gdbarch_num_regs (get_regcache_arch (regcache)); r++) |
dc05df57 | 457 | do_windows_store_inferior_registers (regcache, r); |
24e60978 SC |
458 | } |
459 | } | |
460 | ||
3cee93ac CF |
461 | /* Store a new register value into the current thread context */ |
462 | static void | |
28439f5e PA |
463 | windows_store_inferior_registers (struct target_ops *ops, |
464 | struct regcache *regcache, int r) | |
3cee93ac | 465 | { |
2dc38344 | 466 | current_thread = thread_rec (ptid_get_tid (inferior_ptid), TRUE); |
d6dc8049 CF |
467 | /* Check if current_thread exists. Windows sometimes uses a non-existent |
468 | thread id in its events */ | |
3ade5333 | 469 | if (current_thread) |
dc05df57 | 470 | do_windows_store_inferior_registers (regcache, r); |
3cee93ac | 471 | } |
24e60978 | 472 | |
33605d39 CF |
473 | /* Get the name of a given module at at given base address. If base_address |
474 | is zero return the first loaded module (which is always the name of the | |
475 | executable). */ | |
3ee6f623 | 476 | static int |
d3653bf6 | 477 | get_module_name (LPVOID base_address, char *dll_name_ret) |
1e37c281 JM |
478 | { |
479 | DWORD len; | |
480 | MODULEINFO mi; | |
481 | int i; | |
8e860359 | 482 | HMODULE dh_buf[1]; |
33605d39 | 483 | HMODULE *DllHandle = dh_buf; /* Set to temporary storage for initial query */ |
1e37c281 | 484 | DWORD cbNeeded; |
33605d39 CF |
485 | #ifdef __CYGWIN__ |
486 | char pathbuf[PATH_MAX + 1]; /* Temporary storage prior to converting to | |
487 | posix form */ | |
488 | #else | |
489 | char *pathbuf = dll_name_ret; /* Just copy directly to passed-in arg */ | |
490 | #endif | |
1e37c281 | 491 | |
1e37c281 | 492 | cbNeeded = 0; |
33605d39 | 493 | /* Find size of buffer needed to handle list of modules loaded in inferior */ |
2b008701 CF |
494 | if (!EnumProcessModules (current_process_handle, DllHandle, |
495 | sizeof (HMODULE), &cbNeeded) || !cbNeeded) | |
1e37c281 JM |
496 | goto failed; |
497 | ||
33605d39 | 498 | /* Allocate correct amount of space for module list */ |
8e860359 | 499 | DllHandle = (HMODULE *) alloca (cbNeeded); |
1e37c281 JM |
500 | if (!DllHandle) |
501 | goto failed; | |
502 | ||
33605d39 | 503 | /* Get the list of modules */ |
2b008701 | 504 | if (!EnumProcessModules (current_process_handle, DllHandle, cbNeeded, |
33605d39 | 505 | &cbNeeded)) |
1e37c281 JM |
506 | goto failed; |
507 | ||
29fe111d | 508 | for (i = 0; i < (int) (cbNeeded / sizeof (HMODULE)); i++) |
1e37c281 | 509 | { |
33605d39 | 510 | /* Get information on this module */ |
2b008701 CF |
511 | if (!GetModuleInformation (current_process_handle, DllHandle[i], |
512 | &mi, sizeof (mi))) | |
8a3fe4f8 | 513 | error (_("Can't get module info")); |
1e37c281 | 514 | |
d3653bf6 | 515 | if (!base_address || mi.lpBaseOfDll == base_address) |
33605d39 CF |
516 | { |
517 | /* Try to find the name of the given module */ | |
2b008701 CF |
518 | len = GetModuleFileNameExA (current_process_handle, |
519 | DllHandle[i], pathbuf, MAX_PATH); | |
33605d39 CF |
520 | if (len == 0) |
521 | error (_("Error getting dll name: %u."), (unsigned) GetLastError ()); | |
522 | #ifdef __CYGWIN__ | |
523 | /* Cygwin prefers that the path be in /x/y/z format */ | |
524 | cygwin_conv_to_full_posix_path (pathbuf, dll_name_ret); | |
525 | #endif | |
526 | return 1; /* success */ | |
527 | } | |
1e37c281 JM |
528 | } |
529 | ||
530 | failed: | |
531 | dll_name_ret[0] = '\0'; | |
33605d39 | 532 | return 0; /* failure */ |
1e37c281 JM |
533 | } |
534 | ||
450005e7 CF |
535 | /* Encapsulate the information required in a call to |
536 | symbol_file_add_args */ | |
8a892701 CF |
537 | struct safe_symbol_file_add_args |
538 | { | |
539 | char *name; | |
540 | int from_tty; | |
541 | struct section_addr_info *addrs; | |
542 | int mainline; | |
543 | int flags; | |
7c5c87c0 | 544 | struct ui_file *err, *out; |
8a892701 CF |
545 | struct objfile *ret; |
546 | }; | |
547 | ||
02e423b9 | 548 | /* Maintain a linked list of "so" information. */ |
3ee6f623 | 549 | struct lm_info |
02e423b9 | 550 | { |
d3653bf6 | 551 | LPVOID load_addr; |
3ee6f623 CF |
552 | }; |
553 | ||
554 | static struct so_list solib_start, *solib_end; | |
02e423b9 | 555 | |
450005e7 CF |
556 | /* Call symbol_file_add with stderr redirected. We don't care if there |
557 | are errors. */ | |
8a892701 CF |
558 | static int |
559 | safe_symbol_file_add_stub (void *argv) | |
560 | { | |
3ee6f623 | 561 | #define p ((struct safe_symbol_file_add_args *) argv) |
7eedccfa PP |
562 | const int add_flags = ((p->from_tty ? SYMFILE_VERBOSE : 0) |
563 | | (p->mainline ? SYMFILE_MAINLINE : 0)); | |
564 | p->ret = symbol_file_add (p->name, add_flags, p->addrs, p->flags); | |
8a892701 CF |
565 | return !!p->ret; |
566 | #undef p | |
567 | } | |
568 | ||
450005e7 | 569 | /* Restore gdb's stderr after calling symbol_file_add */ |
8a892701 | 570 | static void |
7c5c87c0 | 571 | safe_symbol_file_add_cleanup (void *p) |
8a892701 | 572 | { |
8e860359 | 573 | #define sp ((struct safe_symbol_file_add_args *)p) |
450005e7 | 574 | gdb_flush (gdb_stderr); |
7c5c87c0 | 575 | gdb_flush (gdb_stdout); |
d3ff4a77 | 576 | ui_file_delete (gdb_stderr); |
7c5c87c0 | 577 | ui_file_delete (gdb_stdout); |
d3ff4a77 | 578 | gdb_stderr = sp->err; |
9d3789f7 | 579 | gdb_stdout = sp->out; |
8e860359 | 580 | #undef sp |
8a892701 CF |
581 | } |
582 | ||
450005e7 | 583 | /* symbol_file_add wrapper that prevents errors from being displayed. */ |
8a892701 CF |
584 | static struct objfile * |
585 | safe_symbol_file_add (char *name, int from_tty, | |
586 | struct section_addr_info *addrs, | |
587 | int mainline, int flags) | |
8a892701 CF |
588 | { |
589 | struct safe_symbol_file_add_args p; | |
590 | struct cleanup *cleanup; | |
591 | ||
7c5c87c0 | 592 | cleanup = make_cleanup (safe_symbol_file_add_cleanup, &p); |
8a892701 | 593 | |
7c5c87c0 CF |
594 | p.err = gdb_stderr; |
595 | p.out = gdb_stdout; | |
450005e7 | 596 | gdb_flush (gdb_stderr); |
7c5c87c0 | 597 | gdb_flush (gdb_stdout); |
d3ff4a77 | 598 | gdb_stderr = ui_file_new (); |
7c5c87c0 | 599 | gdb_stdout = ui_file_new (); |
8a892701 CF |
600 | p.name = name; |
601 | p.from_tty = from_tty; | |
602 | p.addrs = addrs; | |
603 | p.mainline = mainline; | |
604 | p.flags = flags; | |
605 | catch_errors (safe_symbol_file_add_stub, &p, "", RETURN_MASK_ERROR); | |
606 | ||
607 | do_cleanups (cleanup); | |
608 | return p.ret; | |
609 | } | |
610 | ||
de1b3c3d | 611 | static struct so_list * |
dc05df57 | 612 | windows_make_so (const char *name, LPVOID load_addr) |
8e860359 | 613 | { |
3ee6f623 | 614 | struct so_list *so; |
3f8ad85b CF |
615 | char buf[MAX_PATH + 1]; |
616 | char cwd[MAX_PATH + 1]; | |
617 | char *p; | |
618 | WIN32_FIND_DATA w32_fd; | |
619 | HANDLE h = FindFirstFile(name, &w32_fd); | |
5633f842 | 620 | MEMORY_BASIC_INFORMATION m; |
3f8ad85b | 621 | |
6badb179 CF |
622 | if (h == INVALID_HANDLE_VALUE) |
623 | strcpy (buf, name); | |
624 | else | |
3f8ad85b | 625 | { |
c914e0cc CF |
626 | FindClose (h); |
627 | strcpy (buf, name); | |
628 | if (GetCurrentDirectory (MAX_PATH + 1, cwd)) | |
629 | { | |
630 | p = strrchr (buf, '\\'); | |
631 | if (p) | |
632 | p[1] = '\0'; | |
633 | SetCurrentDirectory (buf); | |
634 | GetFullPathName (w32_fd.cFileName, MAX_PATH, buf, &p); | |
635 | SetCurrentDirectory (cwd); | |
636 | } | |
3f8ad85b CF |
637 | } |
638 | ||
3ee6f623 CF |
639 | if (strcasecmp (buf, "ntdll.dll") == 0) |
640 | { | |
641 | GetSystemDirectory (buf, sizeof (buf)); | |
642 | strcat (buf, "\\ntdll.dll"); | |
643 | } | |
3929abe9 | 644 | so = XZALLOC (struct so_list); |
3ee6f623 CF |
645 | so->lm_info = (struct lm_info *) xmalloc (sizeof (struct lm_info)); |
646 | so->lm_info->load_addr = load_addr; | |
de1b3c3d | 647 | strcpy (so->so_original_name, name); |
10325bc5 PA |
648 | #ifndef __CYGWIN__ |
649 | strcpy (so->so_name, buf); | |
650 | #else | |
651 | cygwin_conv_to_posix_path (buf, so->so_name); | |
de1b3c3d PA |
652 | /* Record cygwin1.dll .text start/end. */ |
653 | p = strchr (so->so_name, '\0') - (sizeof ("/cygwin1.dll") - 1); | |
654 | if (p >= so->so_name && strcasecmp (p, "/cygwin1.dll") == 0) | |
655 | { | |
656 | bfd *abfd; | |
657 | asection *text = NULL; | |
658 | CORE_ADDR text_vma; | |
8e860359 | 659 | |
610dd7f9 | 660 | abfd = bfd_openr (so->so_name, "pei-i386"); |
a244bdca | 661 | |
de1b3c3d PA |
662 | if (!abfd) |
663 | return so; | |
664 | ||
665 | if (bfd_check_format (abfd, bfd_object)) | |
666 | text = bfd_get_section_by_name (abfd, ".text"); | |
667 | ||
668 | if (!text) | |
669 | { | |
670 | bfd_close (abfd); | |
671 | return so; | |
672 | } | |
673 | ||
674 | /* The symbols in a dll are offset by 0x1000, which is the the | |
675 | offset from 0 of the first byte in an image - because of the | |
676 | file header and the section alignment. */ | |
dc05df57 CF |
677 | cygwin_load_start = (CORE_ADDR) (uintptr_t) ((char *) load_addr + 0x1000); |
678 | cygwin_load_end = cygwin_load_start + bfd_section_size (abfd, text); | |
de1b3c3d PA |
679 | |
680 | bfd_close (abfd); | |
681 | } | |
10325bc5 | 682 | #endif |
de1b3c3d PA |
683 | |
684 | return so; | |
8e860359 CF |
685 | } |
686 | ||
3ee6f623 | 687 | static char * |
dfe7f3ac CF |
688 | get_image_name (HANDLE h, void *address, int unicode) |
689 | { | |
690 | static char buf[(2 * MAX_PATH) + 1]; | |
691 | DWORD size = unicode ? sizeof (WCHAR) : sizeof (char); | |
692 | char *address_ptr; | |
693 | int len = 0; | |
694 | char b[2]; | |
5732a500 | 695 | SIZE_T done; |
dfe7f3ac CF |
696 | |
697 | /* Attempt to read the name of the dll that was detected. | |
698 | This is documented to work only when actively debugging | |
699 | a program. It will not work for attached processes. */ | |
700 | if (address == NULL) | |
701 | return NULL; | |
702 | ||
dfe7f3ac CF |
703 | /* See if we could read the address of a string, and that the |
704 | address isn't null. */ | |
9f476a01 | 705 | if (!ReadProcessMemory (h, address, &address_ptr, sizeof (address_ptr), &done) |
6f17862b | 706 | || done != sizeof (address_ptr) || !address_ptr) |
dfe7f3ac CF |
707 | return NULL; |
708 | ||
709 | /* Find the length of the string */ | |
6f17862b CF |
710 | while (ReadProcessMemory (h, address_ptr + len++ * size, &b, size, &done) |
711 | && (b[0] != 0 || b[size - 1] != 0) && done == size) | |
712 | continue; | |
dfe7f3ac CF |
713 | |
714 | if (!unicode) | |
715 | ReadProcessMemory (h, address_ptr, buf, len, &done); | |
716 | else | |
717 | { | |
718 | WCHAR *unicode_address = (WCHAR *) alloca (len * sizeof (WCHAR)); | |
719 | ReadProcessMemory (h, address_ptr, unicode_address, len * sizeof (WCHAR), | |
720 | &done); | |
721 | ||
722 | WideCharToMultiByte (CP_ACP, 0, unicode_address, len, buf, len, 0, 0); | |
723 | } | |
724 | ||
725 | return buf; | |
726 | } | |
727 | ||
24e60978 SC |
728 | /* Wait for child to do something. Return pid of child, or -1 in case |
729 | of error; store status through argument pointer OURSTATUS. */ | |
1750a5ef | 730 | static int |
0a65a603 | 731 | handle_load_dll (void *dummy) |
24e60978 | 732 | { |
3a4b77d8 | 733 | LOAD_DLL_DEBUG_INFO *event = ¤t_event.u.LoadDll; |
3cee93ac | 734 | char dll_buf[MAX_PATH + 1]; |
450005e7 | 735 | char *dll_name = NULL; |
3cee93ac | 736 | |
3a4b77d8 | 737 | dll_buf[0] = dll_buf[sizeof (dll_buf) - 1] = '\0'; |
3cee93ac | 738 | |
d3653bf6 | 739 | if (!get_module_name (event->lpBaseOfDll, dll_buf)) |
8e860359 | 740 | dll_buf[0] = dll_buf[sizeof (dll_buf) - 1] = '\0'; |
3cee93ac | 741 | |
1e37c281 | 742 | dll_name = dll_buf; |
24e60978 | 743 | |
dfe7f3ac | 744 | if (*dll_name == '\0') |
de1b3c3d PA |
745 | dll_name = get_image_name (current_process_handle, |
746 | event->lpImageName, event->fUnicode); | |
3cee93ac CF |
747 | if (!dll_name) |
748 | return 1; | |
749 | ||
dc05df57 | 750 | solib_end->next = windows_make_so (dll_name, event->lpBaseOfDll); |
de1b3c3d | 751 | solib_end = solib_end->next; |
450005e7 | 752 | |
a74ce742 PM |
753 | DEBUG_EVENTS (("gdb: Loading dll \"%s\" at %s.\n", solib_end->so_name, |
754 | host_address_to_string (solib_end->lm_info->load_addr))); | |
7488902c | 755 | |
450005e7 CF |
756 | return 1; |
757 | } | |
758 | ||
3ee6f623 | 759 | static void |
dc05df57 | 760 | windows_free_so (struct so_list *so) |
3ee6f623 | 761 | { |
3ee6f623 CF |
762 | if (so->lm_info) |
763 | xfree (so->lm_info); | |
de1b3c3d | 764 | xfree (so); |
3cb8e7f6 CF |
765 | } |
766 | ||
d3ff4a77 | 767 | static int |
0a65a603 | 768 | handle_unload_dll (void *dummy) |
d3ff4a77 | 769 | { |
d3653bf6 | 770 | LPVOID lpBaseOfDll = current_event.u.UnloadDll.lpBaseOfDll; |
3ee6f623 | 771 | struct so_list *so; |
d3ff4a77 CF |
772 | |
773 | for (so = &solib_start; so->next != NULL; so = so->next) | |
3ee6f623 | 774 | if (so->next->lm_info->load_addr == lpBaseOfDll) |
d3ff4a77 | 775 | { |
3ee6f623 | 776 | struct so_list *sodel = so->next; |
d3ff4a77 CF |
777 | so->next = sodel->next; |
778 | if (!so->next) | |
779 | solib_end = so; | |
7488902c PM |
780 | DEBUG_EVENTS (("gdb: Unloading dll \"%s\".\n", sodel->so_name)); |
781 | ||
dc05df57 | 782 | windows_free_so (sodel); |
3929abe9 | 783 | solib_add (NULL, 0, NULL, auto_solib_add); |
d3ff4a77 CF |
784 | return 1; |
785 | } | |
3929abe9 | 786 | |
ecc13e53 JB |
787 | /* We did not find any DLL that was previously loaded at this address, |
788 | so register a complaint. We do not report an error, because we have | |
789 | observed that this may be happening under some circumstances. For | |
790 | instance, running 32bit applications on x64 Windows causes us to receive | |
791 | 4 mysterious UNLOAD_DLL_DEBUG_EVENTs during the startup phase (these | |
792 | events are apparently caused by the WOW layer, the interface between | |
793 | 32bit and 64bit worlds). */ | |
794 | complaint (&symfile_complaints, _("dll starting at %s not found."), | |
795 | host_address_to_string (lpBaseOfDll)); | |
d3ff4a77 CF |
796 | |
797 | return 0; | |
798 | } | |
799 | ||
450005e7 | 800 | /* Clear list of loaded DLLs. */ |
3ee6f623 | 801 | static void |
dc05df57 | 802 | windows_clear_solib (void) |
450005e7 | 803 | { |
450005e7 CF |
804 | solib_start.next = NULL; |
805 | solib_end = &solib_start; | |
450005e7 | 806 | } |
295732ea | 807 | |
450005e7 CF |
808 | /* Load DLL symbol info. */ |
809 | void | |
7470a420 | 810 | dll_symbol_command (char *args, int from_tty) |
450005e7 | 811 | { |
8e860359 | 812 | int n; |
450005e7 | 813 | dont_repeat (); |
8e860359 | 814 | |
450005e7 | 815 | if (args == NULL) |
8a3fe4f8 | 816 | error (_("dll-symbols requires a file name")); |
450005e7 | 817 | |
8e860359 CF |
818 | n = strlen (args); |
819 | if (n > 4 && strcasecmp (args + n - 4, ".dll") != 0) | |
820 | { | |
821 | char *newargs = (char *) alloca (n + 4 + 1); | |
822 | strcpy (newargs, args); | |
823 | strcat (newargs, ".dll"); | |
824 | args = newargs; | |
825 | } | |
826 | ||
7470a420 | 827 | safe_symbol_file_add (args, from_tty, NULL, 0, OBJF_SHARED | OBJF_USERLOADED); |
8e860359 | 828 | } |
450005e7 | 829 | |
3cee93ac CF |
830 | /* Handle DEBUG_STRING output from child process. |
831 | Cygwin prepends its messages with a "cygwin:". Interpret this as | |
832 | a Cygwin signal. Otherwise just print the string as a warning. */ | |
833 | static int | |
834 | handle_output_debug_string (struct target_waitstatus *ourstatus) | |
835 | { | |
a244bdca CF |
836 | char *s = NULL; |
837 | int retval = 0; | |
3cee93ac CF |
838 | |
839 | if (!target_read_string | |
2c647436 PM |
840 | ((CORE_ADDR) (uintptr_t) current_event.u.DebugString.lpDebugStringData, |
841 | &s, 1024, 0) | |
3cee93ac | 842 | || !s || !*s) |
a244bdca CF |
843 | /* nothing to do */; |
844 | else if (strncmp (s, _CYGWIN_SIGNAL_STRING, sizeof (_CYGWIN_SIGNAL_STRING) - 1) != 0) | |
3cee93ac | 845 | { |
10325bc5 | 846 | #ifdef __CYGWIN__ |
d3a09475 | 847 | if (strncmp (s, "cYg", 3) != 0) |
10325bc5 | 848 | #endif |
8a3fe4f8 | 849 | warning (("%s"), s); |
3cee93ac | 850 | } |
cb832706 | 851 | #ifdef __COPY_CONTEXT_SIZE |
d3a09475 | 852 | else |
3cee93ac | 853 | { |
a244bdca CF |
854 | /* Got a cygwin signal marker. A cygwin signal is followed by the signal number |
855 | itself and then optionally followed by the thread id and address to saved context | |
856 | within the DLL. If these are supplied, then the given thread is assumed to have | |
857 | issued the signal and the context from the thread is assumed to be stored at the | |
858 | given address in the inferior. Tell gdb to treat this like a real signal. */ | |
3cee93ac | 859 | char *p; |
3929abe9 | 860 | int sig = strtol (s + sizeof (_CYGWIN_SIGNAL_STRING) - 1, &p, 0); |
a244bdca | 861 | int gotasig = target_signal_from_host (sig); |
0714f9bf SS |
862 | ourstatus->value.sig = gotasig; |
863 | if (gotasig) | |
a244bdca CF |
864 | { |
865 | LPCVOID x; | |
866 | DWORD n; | |
867 | ourstatus->kind = TARGET_WAITKIND_STOPPED; | |
868 | retval = strtoul (p, &p, 0); | |
869 | if (!retval) | |
870 | retval = main_thread_id; | |
871 | else if ((x = (LPCVOID) strtoul (p, &p, 0)) | |
872 | && ReadProcessMemory (current_process_handle, x, | |
873 | &saved_context, __COPY_CONTEXT_SIZE, &n) | |
874 | && n == __COPY_CONTEXT_SIZE) | |
875 | have_saved_context = 1; | |
876 | current_event.dwThreadId = retval; | |
877 | } | |
3cee93ac | 878 | } |
cb832706 | 879 | #endif |
3cee93ac | 880 | |
a244bdca CF |
881 | if (s) |
882 | xfree (s); | |
883 | return retval; | |
3cee93ac | 884 | } |
24e60978 | 885 | |
c1748f97 PM |
886 | static int |
887 | display_selector (HANDLE thread, DWORD sel) | |
888 | { | |
889 | LDT_ENTRY info; | |
890 | if (GetThreadSelectorEntry (thread, sel, &info)) | |
891 | { | |
892 | int base, limit; | |
893 | printf_filtered ("0x%03lx: ", sel); | |
894 | if (!info.HighWord.Bits.Pres) | |
baa93fa6 CF |
895 | { |
896 | puts_filtered ("Segment not present\n"); | |
897 | return 0; | |
898 | } | |
c1748f97 PM |
899 | base = (info.HighWord.Bits.BaseHi << 24) + |
900 | (info.HighWord.Bits.BaseMid << 16) | |
901 | + info.BaseLow; | |
902 | limit = (info.HighWord.Bits.LimitHi << 16) + info.LimitLow; | |
903 | if (info.HighWord.Bits.Granularity) | |
caad7706 | 904 | limit = (limit << 12) | 0xfff; |
c1748f97 PM |
905 | printf_filtered ("base=0x%08x limit=0x%08x", base, limit); |
906 | if (info.HighWord.Bits.Default_Big) | |
baa93fa6 | 907 | puts_filtered(" 32-bit "); |
c1748f97 | 908 | else |
baa93fa6 | 909 | puts_filtered(" 16-bit "); |
c1748f97 PM |
910 | switch ((info.HighWord.Bits.Type & 0xf) >> 1) |
911 | { | |
912 | case 0: | |
baa93fa6 CF |
913 | puts_filtered ("Data (Read-Only, Exp-up"); |
914 | break; | |
c1748f97 | 915 | case 1: |
baa93fa6 CF |
916 | puts_filtered ("Data (Read/Write, Exp-up"); |
917 | break; | |
c1748f97 | 918 | case 2: |
baa93fa6 CF |
919 | puts_filtered ("Unused segment ("); |
920 | break; | |
c1748f97 | 921 | case 3: |
baa93fa6 CF |
922 | puts_filtered ("Data (Read/Write, Exp-down"); |
923 | break; | |
c1748f97 | 924 | case 4: |
baa93fa6 CF |
925 | puts_filtered ("Code (Exec-Only, N.Conf"); |
926 | break; | |
c1748f97 | 927 | case 5: |
baa93fa6 | 928 | puts_filtered ("Code (Exec/Read, N.Conf"); |
c1748f97 PM |
929 | break; |
930 | case 6: | |
baa93fa6 | 931 | puts_filtered ("Code (Exec-Only, Conf"); |
c1748f97 PM |
932 | break; |
933 | case 7: | |
baa93fa6 | 934 | puts_filtered ("Code (Exec/Read, Conf"); |
c1748f97 PM |
935 | break; |
936 | default: | |
937 | printf_filtered ("Unknown type 0x%x",info.HighWord.Bits.Type); | |
938 | } | |
939 | if ((info.HighWord.Bits.Type & 0x1) == 0) | |
baa93fa6 | 940 | puts_filtered(", N.Acc"); |
c1748f97 PM |
941 | puts_filtered (")\n"); |
942 | if ((info.HighWord.Bits.Type & 0x10) == 0) | |
943 | puts_filtered("System selector "); | |
944 | printf_filtered ("Priviledge level = %d. ", info.HighWord.Bits.Dpl); | |
945 | if (info.HighWord.Bits.Granularity) | |
baa93fa6 | 946 | puts_filtered ("Page granular.\n"); |
c1748f97 PM |
947 | else |
948 | puts_filtered ("Byte granular.\n"); | |
949 | return 1; | |
950 | } | |
951 | else | |
952 | { | |
953 | printf_filtered ("Invalid selector 0x%lx.\n",sel); | |
954 | return 0; | |
955 | } | |
956 | } | |
957 | ||
958 | static void | |
959 | display_selectors (char * args, int from_tty) | |
960 | { | |
961 | if (!current_thread) | |
962 | { | |
963 | puts_filtered ("Impossible to display selectors now.\n"); | |
964 | return; | |
965 | } | |
966 | if (!args) | |
967 | { | |
968 | ||
969 | puts_filtered ("Selector $cs\n"); | |
970 | display_selector (current_thread->h, | |
baa93fa6 | 971 | current_thread->context.SegCs); |
c1748f97 PM |
972 | puts_filtered ("Selector $ds\n"); |
973 | display_selector (current_thread->h, | |
baa93fa6 | 974 | current_thread->context.SegDs); |
c1748f97 PM |
975 | puts_filtered ("Selector $es\n"); |
976 | display_selector (current_thread->h, | |
baa93fa6 | 977 | current_thread->context.SegEs); |
c1748f97 PM |
978 | puts_filtered ("Selector $ss\n"); |
979 | display_selector (current_thread->h, | |
baa93fa6 | 980 | current_thread->context.SegSs); |
c1748f97 PM |
981 | puts_filtered ("Selector $fs\n"); |
982 | display_selector (current_thread->h, | |
983 | current_thread->context.SegFs); | |
984 | puts_filtered ("Selector $gs\n"); | |
985 | display_selector (current_thread->h, | |
baa93fa6 | 986 | current_thread->context.SegGs); |
c1748f97 PM |
987 | } |
988 | else | |
989 | { | |
990 | int sel; | |
991 | sel = parse_and_eval_long (args); | |
992 | printf_filtered ("Selector \"%s\"\n",args); | |
993 | display_selector (current_thread->h, sel); | |
994 | } | |
995 | } | |
996 | ||
997 | static struct cmd_list_element *info_w32_cmdlist = NULL; | |
998 | ||
999 | static void | |
1000 | info_w32_command (char *args, int from_tty) | |
1001 | { | |
1002 | help_list (info_w32_cmdlist, "info w32 ", class_info, gdb_stdout); | |
1003 | } | |
1004 | ||
1005 | ||
7393af7c | 1006 | #define DEBUG_EXCEPTION_SIMPLE(x) if (debug_exceptions) \ |
a74ce742 PM |
1007 | printf_unfiltered ("gdb: Target exception %s at %s\n", x, \ |
1008 | host_address_to_string (\ | |
1009 | current_event.u.Exception.ExceptionRecord.ExceptionAddress)) | |
7393af7c | 1010 | |
36339ecd | 1011 | static int |
450005e7 | 1012 | handle_exception (struct target_waitstatus *ourstatus) |
24e60978 | 1013 | { |
3cee93ac | 1014 | thread_info *th; |
29fe111d | 1015 | DWORD code = current_event.u.Exception.ExceptionRecord.ExceptionCode; |
3cee93ac | 1016 | |
29fe111d | 1017 | ourstatus->kind = TARGET_WAITKIND_STOPPED; |
8a892701 | 1018 | |
3cee93ac CF |
1019 | /* Record the context of the current thread */ |
1020 | th = thread_rec (current_event.dwThreadId, -1); | |
24e60978 | 1021 | |
29fe111d | 1022 | switch (code) |
24e60978 | 1023 | { |
1ef980b9 | 1024 | case EXCEPTION_ACCESS_VIOLATION: |
7393af7c PM |
1025 | DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_ACCESS_VIOLATION"); |
1026 | ourstatus->value.sig = TARGET_SIGNAL_SEGV; | |
10325bc5 | 1027 | #ifdef __CYGWIN__ |
8da8e0b3 | 1028 | { |
a244bdca CF |
1029 | /* See if the access violation happened within the cygwin DLL itself. Cygwin uses |
1030 | a kind of exception handling to deal with passed-in invalid addresses. gdb | |
1031 | should not treat these as real SEGVs since they will be silently handled by | |
1032 | cygwin. A real SEGV will (theoretically) be caught by cygwin later in the process | |
1033 | and will be sent as a cygwin-specific-signal. So, ignore SEGVs if they show up | |
1034 | within the text segment of the DLL itself. */ | |
8da8e0b3 | 1035 | char *fn; |
dc05df57 | 1036 | CORE_ADDR addr = (CORE_ADDR) (uintptr_t) current_event.u.Exception.ExceptionRecord.ExceptionAddress; |
09280ddf | 1037 | if ((!cygwin_exceptions && (addr >= cygwin_load_start && addr < cygwin_load_end)) |
a244bdca CF |
1038 | || (find_pc_partial_function (addr, &fn, NULL, NULL) |
1039 | && strncmp (fn, "KERNEL32!IsBad", strlen ("KERNEL32!IsBad")) == 0)) | |
8da8e0b3 CF |
1040 | return 0; |
1041 | } | |
10325bc5 | 1042 | #endif |
7393af7c PM |
1043 | break; |
1044 | case STATUS_STACK_OVERFLOW: | |
1045 | DEBUG_EXCEPTION_SIMPLE ("STATUS_STACK_OVERFLOW"); | |
1ef980b9 | 1046 | ourstatus->value.sig = TARGET_SIGNAL_SEGV; |
7393af7c PM |
1047 | break; |
1048 | case STATUS_FLOAT_DENORMAL_OPERAND: | |
1049 | DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_DENORMAL_OPERAND"); | |
1050 | ourstatus->value.sig = TARGET_SIGNAL_FPE; | |
1051 | break; | |
1052 | case EXCEPTION_ARRAY_BOUNDS_EXCEEDED: | |
1053 | DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_ARRAY_BOUNDS_EXCEEDED"); | |
1054 | ourstatus->value.sig = TARGET_SIGNAL_FPE; | |
1055 | break; | |
1056 | case STATUS_FLOAT_INEXACT_RESULT: | |
1057 | DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_INEXACT_RESULT"); | |
1058 | ourstatus->value.sig = TARGET_SIGNAL_FPE; | |
1059 | break; | |
1060 | case STATUS_FLOAT_INVALID_OPERATION: | |
1061 | DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_INVALID_OPERATION"); | |
1062 | ourstatus->value.sig = TARGET_SIGNAL_FPE; | |
1063 | break; | |
1064 | case STATUS_FLOAT_OVERFLOW: | |
1065 | DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_OVERFLOW"); | |
1066 | ourstatus->value.sig = TARGET_SIGNAL_FPE; | |
1067 | break; | |
1068 | case STATUS_FLOAT_STACK_CHECK: | |
1069 | DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_STACK_CHECK"); | |
1070 | ourstatus->value.sig = TARGET_SIGNAL_FPE; | |
1ef980b9 | 1071 | break; |
3b7c8b74 | 1072 | case STATUS_FLOAT_UNDERFLOW: |
7393af7c PM |
1073 | DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_UNDERFLOW"); |
1074 | ourstatus->value.sig = TARGET_SIGNAL_FPE; | |
1075 | break; | |
3b7c8b74 | 1076 | case STATUS_FLOAT_DIVIDE_BY_ZERO: |
7393af7c PM |
1077 | DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_DIVIDE_BY_ZERO"); |
1078 | ourstatus->value.sig = TARGET_SIGNAL_FPE; | |
1079 | break; | |
3b7c8b74 | 1080 | case STATUS_INTEGER_DIVIDE_BY_ZERO: |
7393af7c | 1081 | DEBUG_EXCEPTION_SIMPLE ("STATUS_INTEGER_DIVIDE_BY_ZERO"); |
3b7c8b74 | 1082 | ourstatus->value.sig = TARGET_SIGNAL_FPE; |
3b7c8b74 | 1083 | break; |
7393af7c PM |
1084 | case STATUS_INTEGER_OVERFLOW: |
1085 | DEBUG_EXCEPTION_SIMPLE ("STATUS_INTEGER_OVERFLOW"); | |
1086 | ourstatus->value.sig = TARGET_SIGNAL_FPE; | |
1ef980b9 SC |
1087 | break; |
1088 | case EXCEPTION_BREAKPOINT: | |
7393af7c | 1089 | DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_BREAKPOINT"); |
1ef980b9 SC |
1090 | ourstatus->value.sig = TARGET_SIGNAL_TRAP; |
1091 | break; | |
1092 | case DBG_CONTROL_C: | |
7393af7c | 1093 | DEBUG_EXCEPTION_SIMPLE ("DBG_CONTROL_C"); |
1ef980b9 | 1094 | ourstatus->value.sig = TARGET_SIGNAL_INT; |
5b421780 PM |
1095 | break; |
1096 | case DBG_CONTROL_BREAK: | |
7393af7c | 1097 | DEBUG_EXCEPTION_SIMPLE ("DBG_CONTROL_BREAK"); |
5b421780 | 1098 | ourstatus->value.sig = TARGET_SIGNAL_INT; |
1ef980b9 SC |
1099 | break; |
1100 | case EXCEPTION_SINGLE_STEP: | |
7393af7c | 1101 | DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_SINGLE_STEP"); |
1ef980b9 SC |
1102 | ourstatus->value.sig = TARGET_SIGNAL_TRAP; |
1103 | break; | |
8227c82d | 1104 | case EXCEPTION_ILLEGAL_INSTRUCTION: |
7393af7c PM |
1105 | DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_ILLEGAL_INSTRUCTION"); |
1106 | ourstatus->value.sig = TARGET_SIGNAL_ILL; | |
1107 | break; | |
1108 | case EXCEPTION_PRIV_INSTRUCTION: | |
1109 | DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_PRIV_INSTRUCTION"); | |
1110 | ourstatus->value.sig = TARGET_SIGNAL_ILL; | |
1111 | break; | |
1112 | case EXCEPTION_NONCONTINUABLE_EXCEPTION: | |
1113 | DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_NONCONTINUABLE_EXCEPTION"); | |
8227c82d CF |
1114 | ourstatus->value.sig = TARGET_SIGNAL_ILL; |
1115 | break; | |
1ef980b9 | 1116 | default: |
a244bdca | 1117 | /* Treat unhandled first chance exceptions specially. */ |
02e423b9 | 1118 | if (current_event.u.Exception.dwFirstChance) |
a244bdca | 1119 | return -1; |
a74ce742 PM |
1120 | printf_unfiltered ("gdb: unknown target exception 0x%08lx at %s\n", |
1121 | current_event.u.Exception.ExceptionRecord.ExceptionCode, | |
1122 | host_address_to_string ( | |
1123 | current_event.u.Exception.ExceptionRecord.ExceptionAddress)); | |
24e60978 | 1124 | ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN; |
1ef980b9 | 1125 | break; |
24e60978 | 1126 | } |
24e60978 | 1127 | exception_count++; |
7393af7c | 1128 | last_sig = ourstatus->value.sig; |
36339ecd | 1129 | return 1; |
24e60978 SC |
1130 | } |
1131 | ||
3cee93ac CF |
1132 | /* Resume all artificially suspended threads if we are continuing |
1133 | execution */ | |
1134 | static BOOL | |
dc05df57 | 1135 | windows_continue (DWORD continue_status, int id) |
3cee93ac CF |
1136 | { |
1137 | int i; | |
1138 | thread_info *th; | |
1139 | BOOL res; | |
1140 | ||
1c1238a5 | 1141 | DEBUG_EVENTS (("ContinueDebugEvent (cpid=%ld, ctid=%lx, %s);\n", |
7393af7c | 1142 | current_event.dwProcessId, current_event.dwThreadId, |
dfe7f3ac | 1143 | continue_status == DBG_CONTINUE ? |
7393af7c | 1144 | "DBG_CONTINUE" : "DBG_EXCEPTION_NOT_HANDLED")); |
6537bb24 PA |
1145 | |
1146 | for (th = &thread_head; (th = th->next) != NULL;) | |
1147 | if ((id == -1 || id == (int) th->id) | |
1148 | && th->suspended) | |
1149 | { | |
1150 | if (debug_registers_changed) | |
1151 | { | |
1152 | th->context.ContextFlags |= CONTEXT_DEBUG_REGISTERS; | |
1153 | th->context.Dr0 = dr[0]; | |
1154 | th->context.Dr1 = dr[1]; | |
1155 | th->context.Dr2 = dr[2]; | |
1156 | th->context.Dr3 = dr[3]; | |
1157 | th->context.Dr6 = DR6_CLEAR_VALUE; | |
1158 | th->context.Dr7 = dr[7]; | |
1159 | } | |
1160 | if (th->context.ContextFlags) | |
1161 | { | |
1162 | CHECK (SetThreadContext (th->h, &th->context)); | |
1163 | th->context.ContextFlags = 0; | |
1164 | } | |
1165 | if (th->suspended > 0) | |
1166 | (void) ResumeThread (th->h); | |
1167 | th->suspended = 0; | |
1168 | } | |
1169 | ||
0714f9bf SS |
1170 | res = ContinueDebugEvent (current_event.dwProcessId, |
1171 | current_event.dwThreadId, | |
1172 | continue_status); | |
3cee93ac | 1173 | |
fa4ba8da | 1174 | debug_registers_changed = 0; |
3cee93ac CF |
1175 | return res; |
1176 | } | |
1177 | ||
d6dc8049 CF |
1178 | /* Called in pathological case where Windows fails to send a |
1179 | CREATE_PROCESS_DEBUG_EVENT after an attach. */ | |
3ee6f623 | 1180 | static DWORD |
5439edaa | 1181 | fake_create_process (void) |
3ade5333 CF |
1182 | { |
1183 | current_process_handle = OpenProcess (PROCESS_ALL_ACCESS, FALSE, | |
1184 | current_event.dwProcessId); | |
bf25528d CF |
1185 | if (current_process_handle != NULL) |
1186 | open_process_used = 1; | |
1187 | else | |
1188 | { | |
1189 | error (_("OpenProcess call failed, GetLastError = %lud\n"), | |
1190 | GetLastError ()); | |
1191 | /* We can not debug anything in that case. */ | |
1192 | } | |
3ade5333 | 1193 | main_thread_id = current_event.dwThreadId; |
dc05df57 CF |
1194 | current_thread = windows_add_thread (ptid_build (current_event.dwProcessId, 0, |
1195 | current_event.dwThreadId), | |
1196 | current_event.u.CreateThread.hThread); | |
3ade5333 CF |
1197 | return main_thread_id; |
1198 | } | |
1199 | ||
a244bdca | 1200 | static void |
28439f5e PA |
1201 | windows_resume (struct target_ops *ops, |
1202 | ptid_t ptid, int step, enum target_signal sig) | |
a244bdca CF |
1203 | { |
1204 | thread_info *th; | |
1205 | DWORD continue_status = DBG_CONTINUE; | |
1206 | ||
2dc38344 PA |
1207 | /* A specific PTID means `step only this thread id'. */ |
1208 | int resume_all = ptid_equal (ptid, minus_one_ptid); | |
1209 | ||
1210 | /* If we're continuing all threads, it's the current inferior that | |
1211 | should be handled specially. */ | |
1212 | if (resume_all) | |
1213 | ptid = inferior_ptid; | |
a244bdca CF |
1214 | |
1215 | if (sig != TARGET_SIGNAL_0) | |
1216 | { | |
1217 | if (current_event.dwDebugEventCode != EXCEPTION_DEBUG_EVENT) | |
1218 | { | |
1219 | DEBUG_EXCEPT(("Cannot continue with signal %d here.\n",sig)); | |
1220 | } | |
1221 | else if (sig == last_sig) | |
1222 | continue_status = DBG_EXCEPTION_NOT_HANDLED; | |
1223 | else | |
1224 | #if 0 | |
1225 | /* This code does not seem to work, because | |
1226 | the kernel does probably not consider changes in the ExceptionRecord | |
1227 | structure when passing the exception to the inferior. | |
1228 | Note that this seems possible in the exception handler itself. */ | |
1229 | { | |
1230 | int i; | |
1231 | for (i = 0; xlate[i].them != -1; i++) | |
1232 | if (xlate[i].us == sig) | |
1233 | { | |
1234 | current_event.u.Exception.ExceptionRecord.ExceptionCode = | |
1235 | xlate[i].them; | |
1236 | continue_status = DBG_EXCEPTION_NOT_HANDLED; | |
1237 | break; | |
1238 | } | |
1239 | if (continue_status == DBG_CONTINUE) | |
1240 | { | |
1241 | DEBUG_EXCEPT(("Cannot continue with signal %d.\n",sig)); | |
1242 | } | |
1243 | } | |
1244 | #endif | |
1245 | DEBUG_EXCEPT(("Can only continue with recieved signal %d.\n", | |
1246 | last_sig)); | |
1247 | } | |
1248 | ||
1249 | last_sig = TARGET_SIGNAL_0; | |
1250 | ||
dc05df57 | 1251 | DEBUG_EXEC (("gdb: windows_resume (pid=%d, tid=%ld, step=%d, sig=%d);\n", |
2dc38344 | 1252 | ptid_get_pid (ptid), ptid_get_tid (ptid), step, sig)); |
a244bdca CF |
1253 | |
1254 | /* Get context for currently selected thread */ | |
2dc38344 | 1255 | th = thread_rec (ptid_get_tid (inferior_ptid), FALSE); |
a244bdca CF |
1256 | if (th) |
1257 | { | |
1258 | if (step) | |
1259 | { | |
1260 | /* Single step by setting t bit */ | |
a97b0ac8 UW |
1261 | struct regcache *regcache = get_current_regcache (); |
1262 | struct gdbarch *gdbarch = get_regcache_arch (regcache); | |
1263 | windows_fetch_inferior_registers (ops, regcache, | |
1264 | gdbarch_ps_regnum (gdbarch)); | |
a244bdca CF |
1265 | th->context.EFlags |= FLAG_TRACE_BIT; |
1266 | } | |
1267 | ||
1268 | if (th->context.ContextFlags) | |
1269 | { | |
1270 | if (debug_registers_changed) | |
1271 | { | |
1272 | th->context.Dr0 = dr[0]; | |
1273 | th->context.Dr1 = dr[1]; | |
1274 | th->context.Dr2 = dr[2]; | |
1275 | th->context.Dr3 = dr[3]; | |
6537bb24 | 1276 | th->context.Dr6 = DR6_CLEAR_VALUE; |
a244bdca CF |
1277 | th->context.Dr7 = dr[7]; |
1278 | } | |
1279 | CHECK (SetThreadContext (th->h, &th->context)); | |
1280 | th->context.ContextFlags = 0; | |
1281 | } | |
1282 | } | |
1283 | ||
1284 | /* Allow continuing with the same signal that interrupted us. | |
1285 | Otherwise complain. */ | |
1286 | ||
2dc38344 | 1287 | if (resume_all) |
dc05df57 | 1288 | windows_continue (continue_status, -1); |
2dc38344 | 1289 | else |
dc05df57 | 1290 | windows_continue (continue_status, ptid_get_tid (ptid)); |
a244bdca CF |
1291 | } |
1292 | ||
695de547 CF |
1293 | /* Ctrl-C handler used when the inferior is not run in the same console. The |
1294 | handler is in charge of interrupting the inferior using DebugBreakProcess. | |
1295 | Note that this function is not available prior to Windows XP. In this case | |
1296 | we emit a warning. */ | |
1297 | BOOL WINAPI | |
1298 | ctrl_c_handler (DWORD event_type) | |
1299 | { | |
1300 | const int attach_flag = current_inferior ()->attach_flag; | |
1301 | ||
bb0613a5 PM |
1302 | /* Only handle Ctrl-C and Ctrl-Break events. Ignore others. */ |
1303 | if (event_type != CTRL_C_EVENT && event_type != CTRL_BREAK_EVENT) | |
695de547 CF |
1304 | return FALSE; |
1305 | ||
1306 | /* If the inferior and the debugger share the same console, do nothing as | |
1307 | the inferior has also received the Ctrl-C event. */ | |
1308 | if (!new_console && !attach_flag) | |
1309 | return TRUE; | |
1310 | ||
1311 | if (!DebugBreakProcess (current_process_handle)) | |
1312 | warning (_("\ | |
1313 | Could not interrupt program. Press Ctrl-c in the program console.")); | |
1314 | ||
1315 | /* Return true to tell that Ctrl-C has been handled. */ | |
1316 | return TRUE; | |
1317 | } | |
1318 | ||
8a892701 | 1319 | /* Get the next event from the child. Return 1 if the event requires |
695de547 | 1320 | handling by WFI (or whatever). */ |
1e37c281 | 1321 | static int |
28439f5e PA |
1322 | get_windows_debug_event (struct target_ops *ops, |
1323 | int pid, struct target_waitstatus *ourstatus) | |
1e37c281 JM |
1324 | { |
1325 | BOOL debug_event; | |
8a892701 | 1326 | DWORD continue_status, event_code; |
87a45c96 | 1327 | thread_info *th; |
8a892701 | 1328 | static thread_info dummy_thread_info; |
450005e7 | 1329 | int retval = 0; |
1e37c281 | 1330 | |
7393af7c | 1331 | last_sig = TARGET_SIGNAL_0; |
9d3789f7 | 1332 | |
8a892701 | 1333 | if (!(debug_event = WaitForDebugEvent (¤t_event, 1000))) |
29fe111d | 1334 | goto out; |
1e37c281 JM |
1335 | |
1336 | event_count++; | |
1337 | continue_status = DBG_CONTINUE; | |
1e37c281 | 1338 | |
8a892701 | 1339 | event_code = current_event.dwDebugEventCode; |
450005e7 | 1340 | ourstatus->kind = TARGET_WAITKIND_SPURIOUS; |
87a45c96 | 1341 | th = NULL; |
a244bdca | 1342 | have_saved_context = 0; |
8a892701 CF |
1343 | |
1344 | switch (event_code) | |
1e37c281 JM |
1345 | { |
1346 | case CREATE_THREAD_DEBUG_EVENT: | |
1347 | DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%x code=%s)\n", | |
8a892701 CF |
1348 | (unsigned) current_event.dwProcessId, |
1349 | (unsigned) current_event.dwThreadId, | |
1350 | "CREATE_THREAD_DEBUG_EVENT")); | |
dfe7f3ac | 1351 | if (saw_create != 1) |
3ade5333 | 1352 | { |
181e7f93 PA |
1353 | struct inferior *inf; |
1354 | inf = find_inferior_pid (current_event.dwProcessId); | |
1355 | if (!saw_create && inf->attach_flag) | |
3ade5333 | 1356 | { |
d6dc8049 CF |
1357 | /* Kludge around a Windows bug where first event is a create |
1358 | thread event. Caused when attached process does not have | |
1359 | a main thread. */ | |
3a3e9ee3 | 1360 | retval = fake_create_process (); |
181e7f93 PA |
1361 | if (retval) |
1362 | saw_create++; | |
3ade5333 CF |
1363 | } |
1364 | break; | |
1365 | } | |
1e37c281 | 1366 | /* Record the existence of this thread */ |
450005e7 | 1367 | retval = current_event.dwThreadId; |
dc05df57 | 1368 | th = windows_add_thread (ptid_build (current_event.dwProcessId, 0, |
2dc38344 PA |
1369 | current_event.dwThreadId), |
1370 | current_event.u.CreateThread.hThread); | |
1e37c281 JM |
1371 | break; |
1372 | ||
1373 | case EXIT_THREAD_DEBUG_EVENT: | |
1c1238a5 | 1374 | DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%x code=%s)\n", |
8a892701 CF |
1375 | (unsigned) current_event.dwProcessId, |
1376 | (unsigned) current_event.dwThreadId, | |
1377 | "EXIT_THREAD_DEBUG_EVENT")); | |
87a45c96 CF |
1378 | if (current_event.dwThreadId != main_thread_id) |
1379 | { | |
dc05df57 | 1380 | windows_delete_thread (ptid_build (current_event.dwProcessId, 0, |
2dc38344 | 1381 | current_event.dwThreadId)); |
87a45c96 CF |
1382 | th = &dummy_thread_info; |
1383 | } | |
1e37c281 JM |
1384 | break; |
1385 | ||
1386 | case CREATE_PROCESS_DEBUG_EVENT: | |
1c1238a5 | 1387 | DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%x code=%s)\n", |
8a892701 CF |
1388 | (unsigned) current_event.dwProcessId, |
1389 | (unsigned) current_event.dwThreadId, | |
1390 | "CREATE_PROCESS_DEBUG_EVENT")); | |
700b351b | 1391 | CloseHandle (current_event.u.CreateProcessInfo.hFile); |
dfe7f3ac | 1392 | if (++saw_create != 1) |
bf25528d | 1393 | break; |
1e37c281 | 1394 | |
dfe7f3ac | 1395 | current_process_handle = current_event.u.CreateProcessInfo.hProcess; |
87a45c96 | 1396 | if (main_thread_id) |
695de547 CF |
1397 | windows_delete_thread (ptid_build (current_event.dwProcessId, 0, |
1398 | main_thread_id)); | |
9d3789f7 | 1399 | main_thread_id = current_event.dwThreadId; |
1e37c281 | 1400 | /* Add the main thread */ |
dc05df57 | 1401 | th = windows_add_thread (ptid_build (current_event.dwProcessId, 0, |
695de547 CF |
1402 | current_event.dwThreadId), |
1403 | current_event.u.CreateProcessInfo.hThread); | |
3a3e9ee3 | 1404 | retval = current_event.dwThreadId; |
1e37c281 JM |
1405 | break; |
1406 | ||
1407 | case EXIT_PROCESS_DEBUG_EVENT: | |
1c1238a5 | 1408 | DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%x code=%s)\n", |
8a892701 CF |
1409 | (unsigned) current_event.dwProcessId, |
1410 | (unsigned) current_event.dwThreadId, | |
1411 | "EXIT_PROCESS_DEBUG_EVENT")); | |
16d905e2 CF |
1412 | if (!windows_initialization_done) |
1413 | { | |
1414 | target_terminal_ours (); | |
1415 | target_mourn_inferior (); | |
1416 | error (_("During startup program exited with code 0x%x."), | |
1417 | (unsigned int) current_event.u.ExitProcess.dwExitCode); | |
1418 | } | |
1419 | else if (saw_create == 1) | |
1420 | { | |
1421 | ourstatus->kind = TARGET_WAITKIND_EXITED; | |
1422 | ourstatus->value.integer = current_event.u.ExitProcess.dwExitCode; | |
1423 | retval = main_thread_id; | |
1424 | } | |
8a892701 | 1425 | break; |
1e37c281 JM |
1426 | |
1427 | case LOAD_DLL_DEBUG_EVENT: | |
1c1238a5 | 1428 | DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%x code=%s)\n", |
8a892701 CF |
1429 | (unsigned) current_event.dwProcessId, |
1430 | (unsigned) current_event.dwThreadId, | |
1431 | "LOAD_DLL_DEBUG_EVENT")); | |
700b351b | 1432 | CloseHandle (current_event.u.LoadDll.hFile); |
dfe7f3ac CF |
1433 | if (saw_create != 1) |
1434 | break; | |
8a892701 | 1435 | catch_errors (handle_load_dll, NULL, (char *) "", RETURN_MASK_ALL); |
450005e7 CF |
1436 | ourstatus->kind = TARGET_WAITKIND_LOADED; |
1437 | ourstatus->value.integer = 0; | |
9d3789f7 | 1438 | retval = main_thread_id; |
1e37c281 JM |
1439 | break; |
1440 | ||
1441 | case UNLOAD_DLL_DEBUG_EVENT: | |
1c1238a5 | 1442 | DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%x code=%s)\n", |
8a892701 CF |
1443 | (unsigned) current_event.dwProcessId, |
1444 | (unsigned) current_event.dwThreadId, | |
1445 | "UNLOAD_DLL_DEBUG_EVENT")); | |
dfe7f3ac CF |
1446 | if (saw_create != 1) |
1447 | break; | |
d3ff4a77 | 1448 | catch_errors (handle_unload_dll, NULL, (char *) "", RETURN_MASK_ALL); |
de1b3c3d PA |
1449 | ourstatus->kind = TARGET_WAITKIND_LOADED; |
1450 | ourstatus->value.integer = 0; | |
1451 | retval = main_thread_id; | |
d3ff4a77 | 1452 | break; |
1e37c281 JM |
1453 | |
1454 | case EXCEPTION_DEBUG_EVENT: | |
1c1238a5 | 1455 | DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%x code=%s)\n", |
8a892701 CF |
1456 | (unsigned) current_event.dwProcessId, |
1457 | (unsigned) current_event.dwThreadId, | |
1458 | "EXCEPTION_DEBUG_EVENT")); | |
dfe7f3ac CF |
1459 | if (saw_create != 1) |
1460 | break; | |
a244bdca CF |
1461 | switch (handle_exception (ourstatus)) |
1462 | { | |
1463 | case 0: | |
1464 | continue_status = DBG_EXCEPTION_NOT_HANDLED; | |
1465 | break; | |
1466 | case 1: | |
1467 | retval = current_event.dwThreadId; | |
1468 | break; | |
1469 | case -1: | |
1470 | last_sig = 1; | |
1471 | continue_status = -1; | |
1472 | break; | |
1473 | } | |
1e37c281 JM |
1474 | break; |
1475 | ||
8a892701 | 1476 | case OUTPUT_DEBUG_STRING_EVENT: /* message from the kernel */ |
1c1238a5 | 1477 | DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%x code=%s)\n", |
8a892701 CF |
1478 | (unsigned) current_event.dwProcessId, |
1479 | (unsigned) current_event.dwThreadId, | |
1480 | "OUTPUT_DEBUG_STRING_EVENT")); | |
dfe7f3ac CF |
1481 | if (saw_create != 1) |
1482 | break; | |
a244bdca | 1483 | retval = handle_output_debug_string (ourstatus); |
1e37c281 | 1484 | break; |
9d3789f7 | 1485 | |
1e37c281 | 1486 | default: |
dfe7f3ac CF |
1487 | if (saw_create != 1) |
1488 | break; | |
29fe111d CF |
1489 | printf_unfiltered ("gdb: kernel event for pid=%ld tid=%ld\n", |
1490 | (DWORD) current_event.dwProcessId, | |
1491 | (DWORD) current_event.dwThreadId); | |
1492 | printf_unfiltered (" unknown event code %ld\n", | |
1e37c281 JM |
1493 | current_event.dwDebugEventCode); |
1494 | break; | |
1495 | } | |
1496 | ||
dfe7f3ac | 1497 | if (!retval || saw_create != 1) |
a244bdca CF |
1498 | { |
1499 | if (continue_status == -1) | |
02529b48 | 1500 | windows_resume (ops, minus_one_ptid, 0, 1); |
a244bdca | 1501 | else |
dc05df57 | 1502 | CHECK (windows_continue (continue_status, -1)); |
a244bdca | 1503 | } |
450005e7 | 1504 | else |
9d3789f7 | 1505 | { |
2dc38344 PA |
1506 | inferior_ptid = ptid_build (current_event.dwProcessId, 0, |
1507 | retval); | |
3ade5333 | 1508 | current_thread = th ?: thread_rec (current_event.dwThreadId, TRUE); |
9d3789f7 | 1509 | } |
1e37c281 JM |
1510 | |
1511 | out: | |
450005e7 | 1512 | return retval; |
1e37c281 JM |
1513 | } |
1514 | ||
2dc38344 | 1515 | /* Wait for interesting events to occur in the target process. */ |
39f77062 | 1516 | static ptid_t |
117de6a9 | 1517 | windows_wait (struct target_ops *ops, |
47608cb1 | 1518 | ptid_t ptid, struct target_waitstatus *ourstatus, int options) |
24e60978 | 1519 | { |
2dc38344 | 1520 | int pid = -1; |
39f77062 | 1521 | |
c44537cf CV |
1522 | target_terminal_ours (); |
1523 | ||
24e60978 SC |
1524 | /* We loop when we get a non-standard exception rather than return |
1525 | with a SPURIOUS because resume can try and step or modify things, | |
3cee93ac | 1526 | which needs a current_thread->h. But some of these exceptions mark |
24e60978 SC |
1527 | the birth or death of threads, which mean that the current thread |
1528 | isn't necessarily what you think it is. */ | |
1529 | ||
1530 | while (1) | |
450005e7 | 1531 | { |
c57918b2 | 1532 | int retval; |
2b008701 | 1533 | |
695de547 CF |
1534 | /* If the user presses Ctrl-c while the debugger is waiting |
1535 | for an event, he expects the debugger to interrupt his program | |
1536 | and to get the prompt back. There are two possible situations: | |
1537 | ||
1538 | - The debugger and the program do not share the console, in | |
1539 | which case the Ctrl-c event only reached the debugger. | |
1540 | In that case, the ctrl_c handler will take care of interrupting | |
1541 | the inferior. Note that this case is working starting with | |
1542 | Windows XP. For Windows 2000, Ctrl-C should be pressed in the | |
1543 | inferior console. | |
1544 | ||
1545 | - The debugger and the program share the same console, in which | |
1546 | case both debugger and inferior will receive the Ctrl-c event. | |
1547 | In that case the ctrl_c handler will ignore the event, as the | |
1548 | Ctrl-c event generated inside the inferior will trigger the | |
1549 | expected debug event. | |
1550 | ||
1551 | FIXME: brobecker/2008-05-20: If the inferior receives the | |
1552 | signal first and the delay until GDB receives that signal | |
1553 | is sufficiently long, GDB can sometimes receive the SIGINT | |
1554 | after we have unblocked the CTRL+C handler. This would | |
1555 | lead to the debugger stopping prematurely while handling | |
1556 | the new-thread event that comes with the handling of the SIGINT | |
1557 | inside the inferior, and then stop again immediately when | |
1558 | the user tries to resume the execution in the inferior. | |
1559 | This is a classic race that we should try to fix one day. */ | |
1560 | SetConsoleCtrlHandler (&ctrl_c_handler, TRUE); | |
28439f5e | 1561 | retval = get_windows_debug_event (ops, pid, ourstatus); |
695de547 | 1562 | SetConsoleCtrlHandler (&ctrl_c_handler, FALSE); |
c57918b2 | 1563 | |
450005e7 | 1564 | if (retval) |
2dc38344 | 1565 | return ptid_build (current_event.dwProcessId, 0, retval); |
450005e7 CF |
1566 | else |
1567 | { | |
1568 | int detach = 0; | |
3cee93ac | 1569 | |
98bbd631 AC |
1570 | if (deprecated_ui_loop_hook != NULL) |
1571 | detach = deprecated_ui_loop_hook (0); | |
0714f9bf | 1572 | |
450005e7 | 1573 | if (detach) |
7d85a9c0 | 1574 | windows_kill_inferior (ops); |
450005e7 CF |
1575 | } |
1576 | } | |
24e60978 SC |
1577 | } |
1578 | ||
9d3789f7 | 1579 | static void |
dc05df57 | 1580 | do_initial_windows_stuff (struct target_ops *ops, DWORD pid, int attaching) |
9d3789f7 CF |
1581 | { |
1582 | extern int stop_after_trap; | |
fa4ba8da | 1583 | int i; |
d6b48e9c | 1584 | struct inferior *inf; |
2020b7ab | 1585 | struct thread_info *tp; |
9d3789f7 | 1586 | |
7393af7c | 1587 | last_sig = TARGET_SIGNAL_0; |
9d3789f7 CF |
1588 | event_count = 0; |
1589 | exception_count = 0; | |
bf25528d | 1590 | open_process_used = 0; |
fa4ba8da | 1591 | debug_registers_changed = 0; |
dfe7f3ac | 1592 | debug_registers_used = 0; |
fa4ba8da PM |
1593 | for (i = 0; i < sizeof (dr) / sizeof (dr[0]); i++) |
1594 | dr[i] = 0; | |
10325bc5 | 1595 | #ifdef __CYGWIN__ |
de1b3c3d | 1596 | cygwin_load_start = cygwin_load_end = 0; |
10325bc5 | 1597 | #endif |
9d3789f7 CF |
1598 | current_event.dwProcessId = pid; |
1599 | memset (¤t_event, 0, sizeof (current_event)); | |
0795be10 | 1600 | push_target (ops); |
cb851954 | 1601 | disable_breakpoints_in_shlibs (); |
dc05df57 | 1602 | windows_clear_solib (); |
9d3789f7 CF |
1603 | clear_proceed_status (); |
1604 | init_wait_for_inferior (); | |
1605 | ||
6c95b8df PA |
1606 | inf = current_inferior (); |
1607 | inferior_appeared (inf, pid); | |
181e7f93 | 1608 | inf->attach_flag = attaching; |
7f9f62ba | 1609 | |
9f9d052e PM |
1610 | /* Make the new process the current inferior, so terminal handling |
1611 | can rely on it. When attaching, we don't know about any thread | |
1612 | id here, but that's OK --- nothing should be referencing the | |
dc05df57 | 1613 | current thread until we report an event out of windows_wait. */ |
9f9d052e PM |
1614 | inferior_ptid = pid_to_ptid (pid); |
1615 | ||
c44537cf | 1616 | terminal_init_inferior_with_pgrp (pid); |
9d3789f7 CF |
1617 | target_terminal_inferior (); |
1618 | ||
16d905e2 | 1619 | windows_initialization_done = 0; |
d6b48e9c | 1620 | inf->stop_soon = STOP_QUIETLY; |
9d3789f7 CF |
1621 | while (1) |
1622 | { | |
1623 | stop_after_trap = 1; | |
ae123ec6 | 1624 | wait_for_inferior (0); |
2020b7ab PA |
1625 | tp = inferior_thread (); |
1626 | if (tp->stop_signal != TARGET_SIGNAL_TRAP) | |
1627 | resume (0, tp->stop_signal); | |
9d3789f7 CF |
1628 | else |
1629 | break; | |
1630 | } | |
eff8332b | 1631 | |
16d905e2 | 1632 | windows_initialization_done = 1; |
d6b48e9c | 1633 | inf->stop_soon = NO_STOP_QUIETLY; |
9d3789f7 CF |
1634 | stop_after_trap = 0; |
1635 | return; | |
1636 | } | |
1637 | ||
616a9dc4 CV |
1638 | /* Try to set or remove a user privilege to the current process. Return -1 |
1639 | if that fails, the previous setting of that privilege otherwise. | |
1640 | ||
1641 | This code is copied from the Cygwin source code and rearranged to allow | |
1642 | dynamically loading of the needed symbols from advapi32 which is only | |
1643 | available on NT/2K/XP. */ | |
1644 | static int | |
1645 | set_process_privilege (const char *privilege, BOOL enable) | |
1646 | { | |
616a9dc4 CV |
1647 | HANDLE token_hdl = NULL; |
1648 | LUID restore_priv; | |
1649 | TOKEN_PRIVILEGES new_priv, orig_priv; | |
1650 | int ret = -1; | |
1651 | DWORD size; | |
1652 | ||
616a9dc4 CV |
1653 | if (!OpenProcessToken (GetCurrentProcess (), |
1654 | TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES, | |
1655 | &token_hdl)) | |
1656 | goto out; | |
1657 | ||
418c6cb3 | 1658 | if (!LookupPrivilegeValueA (NULL, privilege, &restore_priv)) |
616a9dc4 CV |
1659 | goto out; |
1660 | ||
1661 | new_priv.PrivilegeCount = 1; | |
1662 | new_priv.Privileges[0].Luid = restore_priv; | |
1663 | new_priv.Privileges[0].Attributes = enable ? SE_PRIVILEGE_ENABLED : 0; | |
1664 | ||
1665 | if (!AdjustTokenPrivileges (token_hdl, FALSE, &new_priv, | |
295732ea | 1666 | sizeof orig_priv, &orig_priv, &size)) |
616a9dc4 CV |
1667 | goto out; |
1668 | #if 0 | |
1669 | /* Disabled, otherwise every `attach' in an unprivileged user session | |
1670 | would raise the "Failed to get SE_DEBUG_NAME privilege" warning in | |
dc05df57 | 1671 | windows_attach(). */ |
616a9dc4 CV |
1672 | /* AdjustTokenPrivileges returns TRUE even if the privilege could not |
1673 | be enabled. GetLastError () returns an correct error code, though. */ | |
1674 | if (enable && GetLastError () == ERROR_NOT_ALL_ASSIGNED) | |
1675 | goto out; | |
1676 | #endif | |
1677 | ||
1678 | ret = orig_priv.Privileges[0].Attributes == SE_PRIVILEGE_ENABLED ? 1 : 0; | |
1679 | ||
1680 | out: | |
1681 | if (token_hdl) | |
1682 | CloseHandle (token_hdl); | |
1683 | ||
1684 | return ret; | |
1685 | } | |
1686 | ||
02cc9f49 | 1687 | /* Attach to process PID, then initialize for debugging it. */ |
24e60978 | 1688 | static void |
dc05df57 | 1689 | windows_attach (struct target_ops *ops, char *args, int from_tty) |
24e60978 SC |
1690 | { |
1691 | BOOL ok; | |
559e75c0 | 1692 | DWORD pid; |
24e60978 | 1693 | |
74164c56 | 1694 | pid = parse_pid_to_attach (args); |
24e60978 | 1695 | |
616a9dc4 CV |
1696 | if (set_process_privilege (SE_DEBUG_NAME, TRUE) < 0) |
1697 | { | |
1698 | printf_unfiltered ("Warning: Failed to get SE_DEBUG_NAME privilege\n"); | |
1699 | printf_unfiltered ("This can cause attach to fail on Windows NT/2K/XP\n"); | |
1700 | } | |
1701 | ||
dc05df57 | 1702 | windows_init_thread_list (); |
9d3789f7 | 1703 | ok = DebugActiveProcess (pid); |
91a175b3 | 1704 | saw_create = 0; |
24e60978 | 1705 | |
10325bc5 | 1706 | #ifdef __CYGWIN__ |
24e60978 | 1707 | if (!ok) |
baa93fa6 CF |
1708 | { |
1709 | /* Try fall back to Cygwin pid */ | |
1710 | pid = cygwin_internal (CW_CYGWIN_PID_TO_WINPID, pid); | |
1711 | ||
1712 | if (pid > 0) | |
1713 | ok = DebugActiveProcess (pid); | |
10325bc5 PA |
1714 | } |
1715 | #endif | |
baa93fa6 | 1716 | |
10325bc5 PA |
1717 | if (!ok) |
1718 | error (_("Can't attach to process.")); | |
24e60978 | 1719 | |
2b008701 | 1720 | DebugSetProcessKillOnExit (FALSE); |
3ade5333 | 1721 | |
24e60978 SC |
1722 | if (from_tty) |
1723 | { | |
1724 | char *exec_file = (char *) get_exec_file (0); | |
1725 | ||
1726 | if (exec_file) | |
1727 | printf_unfiltered ("Attaching to program `%s', %s\n", exec_file, | |
39f77062 | 1728 | target_pid_to_str (pid_to_ptid (pid))); |
24e60978 SC |
1729 | else |
1730 | printf_unfiltered ("Attaching to %s\n", | |
39f77062 | 1731 | target_pid_to_str (pid_to_ptid (pid))); |
24e60978 SC |
1732 | |
1733 | gdb_flush (gdb_stdout); | |
1734 | } | |
1735 | ||
dc05df57 | 1736 | do_initial_windows_stuff (ops, pid, 1); |
9d3789f7 | 1737 | target_terminal_ours (); |
24e60978 SC |
1738 | } |
1739 | ||
24e60978 | 1740 | static void |
dc05df57 | 1741 | windows_detach (struct target_ops *ops, char *args, int from_tty) |
24e60978 | 1742 | { |
02cc9f49 CV |
1743 | int detached = 1; |
1744 | ||
2b008701 CF |
1745 | ptid_t ptid = {-1}; |
1746 | windows_resume (ops, ptid, 0, TARGET_SIGNAL_0); | |
96998ce7 | 1747 | |
2b008701 CF |
1748 | if (!DebugActiveProcessStop (current_event.dwProcessId)) |
1749 | { | |
1750 | error (_("Can't detach process %lu (error %lu)"), | |
1751 | current_event.dwProcessId, GetLastError ()); | |
1752 | detached = 0; | |
02cc9f49 | 1753 | } |
2b008701 CF |
1754 | DebugSetProcessKillOnExit (FALSE); |
1755 | ||
02cc9f49 | 1756 | if (detached && from_tty) |
24e60978 SC |
1757 | { |
1758 | char *exec_file = get_exec_file (0); | |
1759 | if (exec_file == 0) | |
1760 | exec_file = ""; | |
02cc9f49 CV |
1761 | printf_unfiltered ("Detaching from program: %s, Pid %lu\n", exec_file, |
1762 | current_event.dwProcessId); | |
24e60978 SC |
1763 | gdb_flush (gdb_stdout); |
1764 | } | |
7f9f62ba | 1765 | |
39f77062 | 1766 | inferior_ptid = null_ptid; |
7f9f62ba PA |
1767 | detach_inferior (current_event.dwProcessId); |
1768 | ||
0795be10 | 1769 | unpush_target (ops); |
24e60978 SC |
1770 | } |
1771 | ||
3ee6f623 | 1772 | static char * |
dc05df57 | 1773 | windows_pid_to_exec_file (int pid) |
47216e51 | 1774 | { |
47216e51 | 1775 | static char path[MAX_PATH + 1]; |
10325bc5 PA |
1776 | |
1777 | #ifdef __CYGWIN__ | |
33605d39 CF |
1778 | /* Try to find exe name as symlink target of /proc/<pid>/exe */ |
1779 | int nchars; | |
1780 | char procexe[sizeof ("/proc/4294967295/exe")]; | |
2dc38344 | 1781 | sprintf (procexe, "/proc/%u/exe", pid); |
33605d39 CF |
1782 | nchars = readlink (procexe, path, sizeof(path)); |
1783 | if (nchars > 0 && nchars < sizeof (path)) | |
47216e51 | 1784 | { |
33605d39 CF |
1785 | path[nchars] = '\0'; /* Got it */ |
1786 | return path; | |
47216e51 | 1787 | } |
10325bc5 PA |
1788 | #endif |
1789 | ||
33605d39 CF |
1790 | /* If we get here then either Cygwin is hosed, this isn't a Cygwin version |
1791 | of gdb, or we're trying to debug a non-Cygwin windows executable. */ | |
1792 | if (!get_module_name (0, path)) | |
1793 | path[0] = '\0'; | |
1794 | ||
1795 | return path; | |
47216e51 CV |
1796 | } |
1797 | ||
24e60978 SC |
1798 | /* Print status information about what we're accessing. */ |
1799 | ||
1800 | static void | |
dc05df57 | 1801 | windows_files_info (struct target_ops *ignore) |
24e60978 | 1802 | { |
181e7f93 PA |
1803 | struct inferior *inf = current_inferior (); |
1804 | ||
24e60978 | 1805 | printf_unfiltered ("\tUsing the running image of %s %s.\n", |
181e7f93 PA |
1806 | inf->attach_flag ? "attached" : "child", |
1807 | target_pid_to_str (inferior_ptid)); | |
24e60978 SC |
1808 | } |
1809 | ||
24e60978 | 1810 | static void |
dc05df57 | 1811 | windows_open (char *arg, int from_tty) |
24e60978 | 1812 | { |
8a3fe4f8 | 1813 | error (_("Use the \"run\" command to start a Unix child process.")); |
24e60978 SC |
1814 | } |
1815 | ||
dc05df57 | 1816 | /* Start an inferior windows child process and sets inferior_ptid to its pid. |
24e60978 SC |
1817 | EXEC_FILE is the file to run. |
1818 | ALLARGS is a string containing the arguments to the program. | |
1819 | ENV is the environment vector to pass. Errors reported with error(). */ | |
1820 | ||
24e60978 | 1821 | static void |
dc05df57 | 1822 | windows_create_inferior (struct target_ops *ops, char *exec_file, |
136d6dae | 1823 | char *allargs, char **in_env, int from_tty) |
24e60978 | 1824 | { |
24e60978 SC |
1825 | STARTUPINFO si; |
1826 | PROCESS_INFORMATION pi; | |
24e60978 SC |
1827 | BOOL ret; |
1828 | DWORD flags; | |
eb708f2e | 1829 | char *args; |
dfe7f3ac CF |
1830 | char real_path[MAXPATHLEN]; |
1831 | char *toexec; | |
349b409f CF |
1832 | char shell[MAX_PATH + 1]; /* Path to shell */ |
1833 | const char *sh; | |
41b4aadc | 1834 | #ifdef __CYGWIN__ |
2becadee CF |
1835 | int tty; |
1836 | int ostdin, ostdout, ostderr; | |
41b4aadc CF |
1837 | #else |
1838 | HANDLE tty; | |
1839 | #endif | |
3cb3b8df | 1840 | const char *inferior_io_terminal = get_inferior_io_terminal (); |
24e60978 SC |
1841 | |
1842 | if (!exec_file) | |
8a3fe4f8 | 1843 | error (_("No executable specified, use `target exec'.")); |
24e60978 SC |
1844 | |
1845 | memset (&si, 0, sizeof (si)); | |
1846 | si.cb = sizeof (si); | |
1847 | ||
10325bc5 | 1848 | #ifdef __CYGWIN__ |
349b409f | 1849 | if (!useshell) |
dfe7f3ac CF |
1850 | { |
1851 | flags = DEBUG_ONLY_THIS_PROCESS; | |
1852 | cygwin_conv_to_win32_path (exec_file, real_path); | |
1853 | toexec = real_path; | |
1854 | } | |
1855 | else | |
1856 | { | |
349b409f CF |
1857 | char *newallargs; |
1858 | sh = getenv ("SHELL"); | |
1859 | if (!sh) | |
1860 | sh = "/bin/sh"; | |
1861 | cygwin_conv_to_win32_path (sh, shell); | |
1862 | newallargs = alloca (sizeof (" -c 'exec '") + strlen (exec_file) | |
1863 | + strlen (allargs) + 2); | |
dfe7f3ac CF |
1864 | sprintf (newallargs, " -c 'exec %s %s'", exec_file, allargs); |
1865 | allargs = newallargs; | |
1866 | toexec = shell; | |
1867 | flags = DEBUG_PROCESS; | |
1868 | } | |
10325bc5 PA |
1869 | #else |
1870 | toexec = exec_file; | |
1871 | flags = DEBUG_ONLY_THIS_PROCESS; | |
1872 | #endif | |
eb708f2e | 1873 | |
eeb25b8a PM |
1874 | if (new_group) |
1875 | flags |= CREATE_NEW_PROCESS_GROUP; | |
1876 | ||
1877 | if (new_console) | |
1878 | flags |= CREATE_NEW_CONSOLE; | |
1879 | ||
dfe7f3ac CF |
1880 | args = alloca (strlen (toexec) + strlen (allargs) + 2); |
1881 | strcpy (args, toexec); | |
eb708f2e SC |
1882 | strcat (args, " "); |
1883 | strcat (args, allargs); | |
1884 | ||
10325bc5 | 1885 | #ifdef __CYGWIN__ |
e88c49c3 | 1886 | /* Prepare the environment vars for CreateProcess. */ |
002c07a9 | 1887 | cygwin_internal (CW_SYNC_WINENV); |
1750a5ef | 1888 | |
2becadee CF |
1889 | if (!inferior_io_terminal) |
1890 | tty = ostdin = ostdout = ostderr = -1; | |
1891 | else | |
1892 | { | |
1893 | tty = open (inferior_io_terminal, O_RDWR | O_NOCTTY); | |
1894 | if (tty < 0) | |
1895 | { | |
1896 | print_sys_errmsg (inferior_io_terminal, errno); | |
1897 | ostdin = ostdout = ostderr = -1; | |
1898 | } | |
1899 | else | |
1900 | { | |
1901 | ostdin = dup (0); | |
1902 | ostdout = dup (1); | |
1903 | ostderr = dup (2); | |
1904 | dup2 (tty, 0); | |
1905 | dup2 (tty, 1); | |
1906 | dup2 (tty, 2); | |
1907 | } | |
1908 | } | |
41b4aadc CF |
1909 | #else |
1910 | if (!inferior_io_terminal) | |
1911 | tty = INVALID_HANDLE_VALUE; | |
1912 | else | |
1913 | { | |
1914 | SECURITY_ATTRIBUTES sa; | |
1915 | sa.nLength = sizeof(sa); | |
1916 | sa.lpSecurityDescriptor = 0; | |
1917 | sa.bInheritHandle = TRUE; | |
1918 | tty = CreateFileA (inferior_io_terminal, GENERIC_READ | GENERIC_WRITE, | |
1919 | 0, &sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); | |
1920 | if (tty == INVALID_HANDLE_VALUE) | |
1921 | warning (_("Warning: Failed to open TTY %s, error %#x."), | |
1922 | inferior_io_terminal, (unsigned) GetLastError ()); | |
1923 | else | |
1924 | { | |
1925 | si.hStdInput = tty; | |
1926 | si.hStdOutput = tty; | |
1927 | si.hStdError = tty; | |
1928 | si.dwFlags |= STARTF_USESTDHANDLES; | |
1929 | } | |
1930 | } | |
10325bc5 | 1931 | #endif |
2becadee | 1932 | |
dc05df57 | 1933 | windows_init_thread_list (); |
1750a5ef | 1934 | ret = CreateProcess (0, |
3a4b77d8 | 1935 | args, /* command line */ |
24e60978 SC |
1936 | NULL, /* Security */ |
1937 | NULL, /* thread */ | |
1938 | TRUE, /* inherit handles */ | |
1939 | flags, /* start flags */ | |
002c07a9 | 1940 | NULL, /* environment */ |
24e60978 SC |
1941 | NULL, /* current directory */ |
1942 | &si, | |
1943 | &pi); | |
10325bc5 PA |
1944 | |
1945 | #ifdef __CYGWIN__ | |
2becadee CF |
1946 | if (tty >= 0) |
1947 | { | |
1948 | close (tty); | |
1949 | dup2 (ostdin, 0); | |
1950 | dup2 (ostdout, 1); | |
1951 | dup2 (ostderr, 2); | |
1952 | close (ostdin); | |
1953 | close (ostdout); | |
1954 | close (ostderr); | |
1955 | } | |
41b4aadc CF |
1956 | #else |
1957 | if (tty != INVALID_HANDLE_VALUE) | |
1958 | CloseHandle (tty); | |
10325bc5 | 1959 | #endif |
2becadee | 1960 | |
24e60978 | 1961 | if (!ret) |
8a3fe4f8 AC |
1962 | error (_("Error creating process %s, (error %d)."), |
1963 | exec_file, (unsigned) GetLastError ()); | |
24e60978 | 1964 | |
c1766e7d PM |
1965 | CloseHandle (pi.hThread); |
1966 | CloseHandle (pi.hProcess); | |
1967 | ||
dfe7f3ac CF |
1968 | if (useshell && shell[0] != '\0') |
1969 | saw_create = -1; | |
1970 | else | |
1971 | saw_create = 0; | |
1972 | ||
dc05df57 | 1973 | do_initial_windows_stuff (ops, pi.dwProcessId, 0); |
d3a09475 | 1974 | |
dc05df57 | 1975 | /* windows_continue (DBG_CONTINUE, -1); */ |
24e60978 SC |
1976 | } |
1977 | ||
1978 | static void | |
dc05df57 | 1979 | windows_mourn_inferior (struct target_ops *ops) |
24e60978 | 1980 | { |
dc05df57 | 1981 | (void) windows_continue (DBG_CONTINUE, -1); |
fa4ba8da | 1982 | i386_cleanup_dregs(); |
bf25528d CF |
1983 | if (open_process_used) |
1984 | { | |
1985 | CHECK (CloseHandle (current_process_handle)); | |
1986 | open_process_used = 0; | |
1987 | } | |
0795be10 | 1988 | unpush_target (ops); |
24e60978 SC |
1989 | generic_mourn_inferior (); |
1990 | } | |
1991 | ||
24e60978 SC |
1992 | /* Send a SIGINT to the process group. This acts just like the user typed a |
1993 | ^C on the controlling terminal. */ | |
1994 | ||
b607efe7 | 1995 | static void |
dc05df57 | 1996 | windows_stop (ptid_t ptid) |
24e60978 | 1997 | { |
1ef980b9 | 1998 | DEBUG_EVENTS (("gdb: GenerateConsoleCtrlEvent (CTRLC_EVENT, 0)\n")); |
1e37c281 | 1999 | CHECK (GenerateConsoleCtrlEvent (CTRL_C_EVENT, current_event.dwProcessId)); |
3a4b77d8 | 2000 | registers_changed (); /* refresh register state */ |
24e60978 SC |
2001 | } |
2002 | ||
3ee6f623 | 2003 | static int |
dc05df57 | 2004 | windows_xfer_memory (CORE_ADDR memaddr, gdb_byte *our, int len, |
0a65a603 AC |
2005 | int write, struct mem_attrib *mem, |
2006 | struct target_ops *target) | |
24e60978 | 2007 | { |
5732a500 | 2008 | SIZE_T done = 0; |
24e60978 SC |
2009 | if (write) |
2010 | { | |
29fe111d | 2011 | DEBUG_MEM (("gdb: write target memory, %d bytes at 0x%08lx\n", |
2c647436 | 2012 | len, (DWORD) (uintptr_t) memaddr)); |
2b008701 | 2013 | if (!WriteProcessMemory (current_process_handle, |
2c647436 | 2014 | (LPVOID) (uintptr_t) memaddr, our, |
6f17862b CF |
2015 | len, &done)) |
2016 | done = 0; | |
2b008701 | 2017 | FlushInstructionCache (current_process_handle, |
2c647436 | 2018 | (LPCVOID) (uintptr_t) memaddr, len); |
24e60978 SC |
2019 | } |
2020 | else | |
2021 | { | |
29fe111d | 2022 | DEBUG_MEM (("gdb: read target memory, %d bytes at 0x%08lx\n", |
2c647436 | 2023 | len, (DWORD) (uintptr_t) memaddr)); |
2b008701 | 2024 | if (!ReadProcessMemory (current_process_handle, |
2c647436 | 2025 | (LPCVOID) (uintptr_t) memaddr, our, |
6f17862b CF |
2026 | len, &done)) |
2027 | done = 0; | |
24e60978 SC |
2028 | } |
2029 | return done; | |
2030 | } | |
2031 | ||
3ee6f623 | 2032 | static void |
7d85a9c0 | 2033 | windows_kill_inferior (struct target_ops *ops) |
24e60978 | 2034 | { |
3cee93ac CF |
2035 | CHECK (TerminateProcess (current_process_handle, 0)); |
2036 | ||
b5edcb45 ILT |
2037 | for (;;) |
2038 | { | |
dc05df57 | 2039 | if (!windows_continue (DBG_CONTINUE, -1)) |
b5edcb45 | 2040 | break; |
3cee93ac | 2041 | if (!WaitForDebugEvent (¤t_event, INFINITE)) |
b5edcb45 | 2042 | break; |
3cee93ac | 2043 | if (current_event.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT) |
b5edcb45 ILT |
2044 | break; |
2045 | } | |
2046 | ||
dc05df57 | 2047 | target_mourn_inferior (); /* or just windows_mourn_inferior? */ |
24e60978 SC |
2048 | } |
2049 | ||
24e60978 | 2050 | static void |
dc05df57 | 2051 | windows_prepare_to_store (struct regcache *regcache) |
24e60978 SC |
2052 | { |
2053 | /* Do nothing, since we can store individual regs */ | |
2054 | } | |
2055 | ||
2056 | static int | |
dc05df57 | 2057 | windows_can_run (void) |
24e60978 SC |
2058 | { |
2059 | return 1; | |
2060 | } | |
2061 | ||
2062 | static void | |
dc05df57 | 2063 | windows_close (int x) |
24e60978 | 2064 | { |
dc05df57 | 2065 | DEBUG_EVENTS (("gdb: windows_close, inferior_ptid=%d\n", |
3bccec63 | 2066 | PIDGET (inferior_ptid))); |
24e60978 | 2067 | } |
1ef980b9 | 2068 | |
3ee6f623 CF |
2069 | /* Convert pid to printable format. */ |
2070 | static char * | |
117de6a9 | 2071 | windows_pid_to_str (struct target_ops *ops, ptid_t ptid) |
24e60978 | 2072 | { |
3ee6f623 | 2073 | static char buf[80]; |
3ee6f623 | 2074 | |
2dc38344 PA |
2075 | if (ptid_get_tid (ptid) != 0) |
2076 | { | |
2077 | snprintf (buf, sizeof (buf), "Thread %d.0x%lx", | |
2078 | ptid_get_pid (ptid), ptid_get_tid (ptid)); | |
2079 | return buf; | |
2080 | } | |
2081 | ||
2082 | return normal_pid_to_str (ptid); | |
3ee6f623 CF |
2083 | } |
2084 | ||
de1b3c3d | 2085 | static LONGEST |
dc05df57 | 2086 | windows_xfer_shared_libraries (struct target_ops *ops, |
de1b3c3d PA |
2087 | enum target_object object, const char *annex, |
2088 | gdb_byte *readbuf, const gdb_byte *writebuf, | |
2089 | ULONGEST offset, LONGEST len) | |
3cb8e7f6 | 2090 | { |
de1b3c3d PA |
2091 | struct obstack obstack; |
2092 | const char *buf; | |
2093 | LONGEST len_avail; | |
3cb8e7f6 | 2094 | struct so_list *so; |
3cb8e7f6 | 2095 | |
de1b3c3d PA |
2096 | if (writebuf) |
2097 | return -1; | |
3cb8e7f6 | 2098 | |
de1b3c3d PA |
2099 | obstack_init (&obstack); |
2100 | obstack_grow_str (&obstack, "<library-list>\n"); | |
2101 | for (so = solib_start.next; so; so = so->next) | |
dc05df57 | 2102 | windows_xfer_shared_library (so->so_name, (CORE_ADDR) (uintptr_t) so->lm_info->load_addr, |
5af949e3 | 2103 | target_gdbarch, &obstack); |
de1b3c3d | 2104 | obstack_grow_str0 (&obstack, "</library-list>\n"); |
3cb8e7f6 | 2105 | |
de1b3c3d PA |
2106 | buf = obstack_finish (&obstack); |
2107 | len_avail = strlen (buf); | |
2108 | if (offset >= len_avail) | |
2109 | return 0; | |
3cb8e7f6 | 2110 | |
de1b3c3d PA |
2111 | if (len > len_avail - offset) |
2112 | len = len_avail - offset; | |
2113 | memcpy (readbuf, buf + offset, len); | |
3cb8e7f6 | 2114 | |
de1b3c3d PA |
2115 | obstack_free (&obstack, NULL); |
2116 | return len; | |
3cb8e7f6 CF |
2117 | } |
2118 | ||
de1b3c3d | 2119 | static LONGEST |
dc05df57 | 2120 | windows_xfer_partial (struct target_ops *ops, enum target_object object, |
de1b3c3d PA |
2121 | const char *annex, gdb_byte *readbuf, |
2122 | const gdb_byte *writebuf, ULONGEST offset, LONGEST len) | |
3cb8e7f6 | 2123 | { |
de1b3c3d | 2124 | switch (object) |
3cb8e7f6 | 2125 | { |
de1b3c3d PA |
2126 | case TARGET_OBJECT_MEMORY: |
2127 | if (readbuf) | |
2128 | return (*ops->deprecated_xfer_memory) (offset, readbuf, | |
244e85c8 | 2129 | len, 0/*read*/, NULL, ops); |
de1b3c3d PA |
2130 | if (writebuf) |
2131 | return (*ops->deprecated_xfer_memory) (offset, (gdb_byte *) writebuf, | |
2132 | len, 1/*write*/, NULL, ops); | |
2133 | return -1; | |
2134 | ||
2135 | case TARGET_OBJECT_LIBRARIES: | |
dc05df57 | 2136 | return windows_xfer_shared_libraries (ops, object, annex, readbuf, |
de1b3c3d | 2137 | writebuf, offset, len); |
3929abe9 | 2138 | |
de1b3c3d PA |
2139 | default: |
2140 | if (ops->beneath != NULL) | |
2141 | return ops->beneath->to_xfer_partial (ops->beneath, object, annex, | |
2142 | readbuf, writebuf, offset, len); | |
2143 | return -1; | |
3929abe9 | 2144 | } |
02c5aecd CF |
2145 | } |
2146 | ||
1e2f1c5c JB |
2147 | static ptid_t |
2148 | windows_get_ada_task_ptid (long lwp, long thread) | |
2149 | { | |
2150 | return ptid_build (ptid_get_pid (inferior_ptid), 0, lwp); | |
2151 | } | |
2152 | ||
3ee6f623 | 2153 | static void |
dc05df57 | 2154 | init_windows_ops (void) |
3ee6f623 | 2155 | { |
dc05df57 CF |
2156 | windows_ops.to_shortname = "child"; |
2157 | windows_ops.to_longname = "Win32 child process"; | |
2158 | windows_ops.to_doc = "Win32 child process (started by the \"run\" command)."; | |
2159 | windows_ops.to_open = windows_open; | |
2160 | windows_ops.to_close = windows_close; | |
2161 | windows_ops.to_attach = windows_attach; | |
2162 | windows_ops.to_attach_no_wait = 1; | |
2163 | windows_ops.to_detach = windows_detach; | |
2164 | windows_ops.to_resume = windows_resume; | |
2165 | windows_ops.to_wait = windows_wait; | |
2166 | windows_ops.to_fetch_registers = windows_fetch_inferior_registers; | |
2167 | windows_ops.to_store_registers = windows_store_inferior_registers; | |
2168 | windows_ops.to_prepare_to_store = windows_prepare_to_store; | |
2169 | windows_ops.deprecated_xfer_memory = windows_xfer_memory; | |
2170 | windows_ops.to_xfer_partial = windows_xfer_partial; | |
2171 | windows_ops.to_files_info = windows_files_info; | |
2172 | windows_ops.to_insert_breakpoint = memory_insert_breakpoint; | |
2173 | windows_ops.to_remove_breakpoint = memory_remove_breakpoint; | |
2174 | windows_ops.to_terminal_init = terminal_init_inferior; | |
2175 | windows_ops.to_terminal_inferior = terminal_inferior; | |
2176 | windows_ops.to_terminal_ours_for_output = terminal_ours_for_output; | |
2177 | windows_ops.to_terminal_ours = terminal_ours; | |
2178 | windows_ops.to_terminal_save_ours = terminal_save_ours; | |
2179 | windows_ops.to_terminal_info = child_terminal_info; | |
2180 | windows_ops.to_kill = windows_kill_inferior; | |
2181 | windows_ops.to_create_inferior = windows_create_inferior; | |
2182 | windows_ops.to_mourn_inferior = windows_mourn_inferior; | |
2183 | windows_ops.to_can_run = windows_can_run; | |
2184 | windows_ops.to_thread_alive = windows_thread_alive; | |
2185 | windows_ops.to_pid_to_str = windows_pid_to_str; | |
2186 | windows_ops.to_stop = windows_stop; | |
2187 | windows_ops.to_stratum = process_stratum; | |
c35b1492 PA |
2188 | windows_ops.to_has_all_memory = default_child_has_all_memory; |
2189 | windows_ops.to_has_memory = default_child_has_memory; | |
2190 | windows_ops.to_has_stack = default_child_has_stack; | |
2191 | windows_ops.to_has_registers = default_child_has_registers; | |
2192 | windows_ops.to_has_execution = default_child_has_execution; | |
dc05df57 | 2193 | windows_ops.to_pid_to_exec_file = windows_pid_to_exec_file; |
1e2f1c5c | 2194 | windows_ops.to_get_ada_task_ptid = windows_get_ada_task_ptid; |
9bb9e8ad | 2195 | |
5aca5a82 PM |
2196 | i386_use_watchpoints (&windows_ops); |
2197 | ||
9bb9e8ad PM |
2198 | i386_dr_low.set_control = cygwin_set_dr7; |
2199 | i386_dr_low.set_addr = cygwin_set_dr; | |
2200 | i386_dr_low.reset_addr = NULL; | |
2201 | i386_dr_low.get_status = cygwin_get_dr6; | |
2202 | ||
2203 | /* i386_dr_low.debug_register_length field is set by | |
2204 | calling i386_set_debug_register_length function | |
2205 | in processor windows specific native file. */ | |
2206 | ||
dc05df57 | 2207 | windows_ops.to_magic = OPS_MAGIC; |
c719b714 | 2208 | } |
24e60978 | 2209 | |
3929abe9 | 2210 | static void |
dc05df57 | 2211 | set_windows_aliases (char *argv0) |
3929abe9 CF |
2212 | { |
2213 | add_info_alias ("dll", "sharedlibrary", 1); | |
2214 | } | |
2215 | ||
24e60978 | 2216 | void |
dc05df57 | 2217 | _initialize_windows_nat (void) |
24e60978 | 2218 | { |
fa58ee11 EZ |
2219 | struct cmd_list_element *c; |
2220 | ||
dc05df57 | 2221 | init_windows_ops (); |
1ef980b9 | 2222 | |
fa58ee11 | 2223 | c = add_com ("dll-symbols", class_files, dll_symbol_command, |
1bedd215 | 2224 | _("Load dll library symbols from FILE.")); |
5ba2abeb | 2225 | set_cmd_completer (c, filename_completer); |
450005e7 CF |
2226 | |
2227 | add_com_alias ("sharedlibrary", "dll-symbols", class_alias, 1); | |
2228 | ||
70992597 PM |
2229 | add_com_alias ("add-shared-symbol-files", "dll-symbols", class_alias, 1); |
2230 | ||
2231 | add_com_alias ("assf", "dll-symbols", class_alias, 1); | |
2232 | ||
10325bc5 | 2233 | #ifdef __CYGWIN__ |
5bf193a2 AC |
2234 | add_setshow_boolean_cmd ("shell", class_support, &useshell, _("\ |
2235 | Set use of shell to start subprocess."), _("\ | |
2236 | Show use of shell to start subprocess."), NULL, | |
2237 | NULL, | |
2238 | NULL, /* FIXME: i18n: */ | |
2239 | &setlist, &showlist); | |
2240 | ||
09280ddf CF |
2241 | add_setshow_boolean_cmd ("cygwin-exceptions", class_support, &cygwin_exceptions, _("\ |
2242 | Break when an exception is detected in the Cygwin DLL itself."), _("\ | |
2243 | Show whether gdb breaks on exceptions in the Cygwin DLL itself."), NULL, | |
2244 | NULL, | |
2245 | NULL, /* FIXME: i18n: */ | |
2246 | &setlist, &showlist); | |
10325bc5 | 2247 | #endif |
09280ddf | 2248 | |
5bf193a2 AC |
2249 | add_setshow_boolean_cmd ("new-console", class_support, &new_console, _("\ |
2250 | Set creation of new console when creating child process."), _("\ | |
2251 | Show creation of new console when creating child process."), NULL, | |
2252 | NULL, | |
2253 | NULL, /* FIXME: i18n: */ | |
2254 | &setlist, &showlist); | |
2255 | ||
2256 | add_setshow_boolean_cmd ("new-group", class_support, &new_group, _("\ | |
2257 | Set creation of new group when creating child process."), _("\ | |
2258 | Show creation of new group when creating child process."), NULL, | |
2259 | NULL, | |
2260 | NULL, /* FIXME: i18n: */ | |
2261 | &setlist, &showlist); | |
2262 | ||
2263 | add_setshow_boolean_cmd ("debugexec", class_support, &debug_exec, _("\ | |
2264 | Set whether to display execution in child process."), _("\ | |
2265 | Show whether to display execution in child process."), NULL, | |
2266 | NULL, | |
2267 | NULL, /* FIXME: i18n: */ | |
2268 | &setlist, &showlist); | |
2269 | ||
2270 | add_setshow_boolean_cmd ("debugevents", class_support, &debug_events, _("\ | |
2271 | Set whether to display kernel events in child process."), _("\ | |
2272 | Show whether to display kernel events in child process."), NULL, | |
2273 | NULL, | |
2274 | NULL, /* FIXME: i18n: */ | |
2275 | &setlist, &showlist); | |
2276 | ||
2277 | add_setshow_boolean_cmd ("debugmemory", class_support, &debug_memory, _("\ | |
2278 | Set whether to display memory accesses in child process."), _("\ | |
2279 | Show whether to display memory accesses in child process."), NULL, | |
2280 | NULL, | |
2281 | NULL, /* FIXME: i18n: */ | |
2282 | &setlist, &showlist); | |
2283 | ||
2284 | add_setshow_boolean_cmd ("debugexceptions", class_support, | |
2285 | &debug_exceptions, _("\ | |
2286 | Set whether to display kernel exceptions in child process."), _("\ | |
2287 | Show whether to display kernel exceptions in child process."), NULL, | |
2288 | NULL, | |
2289 | NULL, /* FIXME: i18n: */ | |
2290 | &setlist, &showlist); | |
1ef980b9 | 2291 | |
c1748f97 | 2292 | add_prefix_cmd ("w32", class_info, info_w32_command, |
1bedd215 | 2293 | _("Print information specific to Win32 debugging."), |
baa93fa6 | 2294 | &info_w32_cmdlist, "info w32 ", 0, &infolist); |
c1748f97 PM |
2295 | |
2296 | add_cmd ("selector", class_info, display_selectors, | |
1a966eab | 2297 | _("Display selectors infos."), |
c1748f97 | 2298 | &info_w32_cmdlist); |
dc05df57 CF |
2299 | add_target (&windows_ops); |
2300 | deprecated_init_ui_hook = set_windows_aliases; | |
24e60978 | 2301 | } |
3cee93ac | 2302 | |
fa4ba8da PM |
2303 | /* Hardware watchpoint support, adapted from go32-nat.c code. */ |
2304 | ||
2305 | /* Pass the address ADDR to the inferior in the I'th debug register. | |
2306 | Here we just store the address in dr array, the registers will be | |
dc05df57 | 2307 | actually set up when windows_continue is called. */ |
9bb9e8ad | 2308 | static void |
fa4ba8da PM |
2309 | cygwin_set_dr (int i, CORE_ADDR addr) |
2310 | { | |
2311 | if (i < 0 || i > 3) | |
2312 | internal_error (__FILE__, __LINE__, | |
e2e0b3e5 | 2313 | _("Invalid register %d in cygwin_set_dr.\n"), i); |
41b4aadc | 2314 | dr[i] = addr; |
fa4ba8da PM |
2315 | debug_registers_changed = 1; |
2316 | debug_registers_used = 1; | |
2317 | } | |
2318 | ||
2319 | /* Pass the value VAL to the inferior in the DR7 debug control | |
2320 | register. Here we just store the address in D_REGS, the watchpoint | |
dc05df57 | 2321 | will be actually set up in windows_wait. */ |
9bb9e8ad PM |
2322 | static void |
2323 | cygwin_set_dr7 (unsigned long val) | |
fa4ba8da | 2324 | { |
9bb9e8ad | 2325 | dr[7] = (CORE_ADDR) val; |
fa4ba8da PM |
2326 | debug_registers_changed = 1; |
2327 | debug_registers_used = 1; | |
2328 | } | |
2329 | ||
2330 | /* Get the value of the DR6 debug status register from the inferior. | |
2331 | Here we just return the value stored in dr[6] | |
2332 | by the last call to thread_rec for current_event.dwThreadId id. */ | |
9bb9e8ad | 2333 | static unsigned long |
fa4ba8da PM |
2334 | cygwin_get_dr6 (void) |
2335 | { | |
9bb9e8ad | 2336 | return (unsigned long) dr[6]; |
fa4ba8da PM |
2337 | } |
2338 | ||
2dc38344 | 2339 | /* Determine if the thread referenced by "ptid" is alive |
3cee93ac | 2340 | by "polling" it. If WaitForSingleObject returns WAIT_OBJECT_0 |
2dc38344 | 2341 | it means that the thread has died. Otherwise it is assumed to be alive. */ |
3cee93ac | 2342 | static int |
28439f5e | 2343 | windows_thread_alive (struct target_ops *ops, ptid_t ptid) |
3cee93ac | 2344 | { |
2dc38344 PA |
2345 | int tid; |
2346 | ||
2347 | gdb_assert (ptid_get_tid (ptid) != 0); | |
2348 | tid = ptid_get_tid (ptid); | |
39f77062 | 2349 | |
2dc38344 | 2350 | return WaitForSingleObject (thread_rec (tid, FALSE)->h, 0) == WAIT_OBJECT_0 ? |
3a4b77d8 | 2351 | FALSE : TRUE; |
3cee93ac CF |
2352 | } |
2353 | ||
2a3d5645 CF |
2354 | void |
2355 | _initialize_check_for_gdb_ini (void) | |
2356 | { | |
2357 | char *homedir; | |
2358 | if (inhibit_gdbinit) | |
2359 | return; | |
2360 | ||
2361 | homedir = getenv ("HOME"); | |
2362 | if (homedir) | |
2363 | { | |
2364 | char *p; | |
2365 | char *oldini = (char *) alloca (strlen (homedir) + | |
2366 | sizeof ("/gdb.ini")); | |
2367 | strcpy (oldini, homedir); | |
2368 | p = strchr (oldini, '\0'); | |
2369 | if (p > oldini && p[-1] != '/') | |
2370 | *p++ = '/'; | |
2371 | strcpy (p, "gdb.ini"); | |
2372 | if (access (oldini, 0) == 0) | |
2373 | { | |
2374 | int len = strlen (oldini); | |
2375 | char *newini = alloca (len + 1); | |
dfe7f3ac | 2376 | sprintf (newini, "%.*s.gdbinit", |
58fa08f0 | 2377 | (int) (len - (sizeof ("gdb.ini") - 1)), oldini); |
8a3fe4f8 | 2378 | warning (_("obsolete '%s' found. Rename to '%s'."), oldini, newini); |
2a3d5645 CF |
2379 | } |
2380 | } | |
2381 | } | |
33605d39 | 2382 | |
2b008701 CF |
2383 | /* Define dummy functions which always return error for the rare cases where |
2384 | these functions could not be found. */ | |
2385 | static BOOL WINAPI | |
2386 | bad_DebugActiveProcessStop (DWORD w) | |
2387 | { | |
2388 | return FALSE; | |
2389 | } | |
2390 | static BOOL WINAPI | |
2391 | bad_DebugBreakProcess (HANDLE w) | |
2392 | { | |
2393 | return FALSE; | |
2394 | } | |
2395 | static BOOL WINAPI | |
2396 | bad_DebugSetProcessKillOnExit (BOOL w) | |
2397 | { | |
2398 | return FALSE; | |
2399 | } | |
2400 | static BOOL WINAPI | |
2401 | bad_EnumProcessModules (HANDLE w, HMODULE *x, DWORD y, LPDWORD z) | |
2402 | { | |
2403 | return FALSE; | |
2404 | } | |
2405 | static DWORD WINAPI | |
2406 | bad_GetModuleFileNameExA (HANDLE w, HMODULE x, LPSTR y, DWORD z) | |
2407 | { | |
2408 | return 0; | |
2409 | } | |
2410 | static BOOL WINAPI | |
2411 | bad_GetModuleInformation (HANDLE w, HMODULE x, LPMODULEINFO y, DWORD z) | |
2412 | { | |
2413 | return FALSE; | |
2414 | } | |
2415 | ||
418c6cb3 CF |
2416 | static BOOL WINAPI |
2417 | bad_OpenProcessToken (HANDLE w, DWORD x, PHANDLE y) | |
2418 | { | |
2419 | return FALSE; | |
2420 | } | |
2421 | ||
2b008701 CF |
2422 | /* Load any functions which may not be available in ancient versions |
2423 | of Windows. */ | |
33605d39 | 2424 | void |
2b008701 | 2425 | _initialize_loadable (void) |
33605d39 | 2426 | { |
2b008701 CF |
2427 | HMODULE hm = NULL; |
2428 | ||
2429 | hm = LoadLibrary ("kernel32.dll"); | |
2430 | if (hm) | |
33605d39 | 2431 | { |
2b008701 CF |
2432 | dyn_DebugActiveProcessStop = (void *) |
2433 | GetProcAddress (hm, "DebugActiveProcessStop"); | |
2434 | dyn_DebugBreakProcess = (void *) | |
2435 | GetProcAddress (hm, "DebugBreakProcess"); | |
2436 | dyn_DebugSetProcessKillOnExit = (void *) | |
2437 | GetProcAddress (hm, "DebugSetProcessKillOnExit"); | |
2438 | } | |
33605d39 | 2439 | |
2b008701 CF |
2440 | /* Set variables to dummy versions of these processes if the function |
2441 | wasn't found in kernel32.dll. */ | |
2442 | if (!dyn_DebugBreakProcess) | |
2443 | dyn_DebugBreakProcess = bad_DebugBreakProcess; | |
2444 | if (!dyn_DebugActiveProcessStop || !dyn_DebugSetProcessKillOnExit) | |
2445 | { | |
2446 | dyn_DebugActiveProcessStop = bad_DebugActiveProcessStop; | |
2447 | dyn_DebugSetProcessKillOnExit = bad_DebugSetProcessKillOnExit; | |
2448 | } | |
33605d39 | 2449 | |
2b008701 CF |
2450 | /* Load optional functions used for retrieving filename information |
2451 | associated with the currently debugged process or its dlls. */ | |
2452 | hm = LoadLibrary ("psapi.dll"); | |
2453 | if (hm) | |
2454 | { | |
2455 | dyn_EnumProcessModules = (void *) | |
2456 | GetProcAddress (hm, "EnumProcessModules"); | |
2457 | dyn_GetModuleInformation = (void *) | |
2458 | GetProcAddress (hm, "GetModuleInformation"); | |
2459 | dyn_GetModuleFileNameExA = (void *) | |
2460 | GetProcAddress (hm, "GetModuleFileNameExA"); | |
33605d39 CF |
2461 | } |
2462 | ||
2b008701 CF |
2463 | if (!dyn_EnumProcessModules || !dyn_GetModuleInformation || !dyn_GetModuleFileNameExA) |
2464 | { | |
2465 | /* Set variables to dummy versions of these processes if the function | |
2466 | wasn't found in psapi.dll. */ | |
2467 | dyn_EnumProcessModules = bad_EnumProcessModules; | |
2468 | dyn_GetModuleInformation = bad_GetModuleInformation; | |
2469 | dyn_GetModuleFileNameExA = bad_GetModuleFileNameExA; | |
2470 | /* This will probably fail on Windows 9x/Me. Let the user know that we're | |
2471 | missing some functionality. */ | |
418c6cb3 CF |
2472 | warning(_("cannot automatically find executable file or library to read symbols.\nUse \"file\" or \"dll\" command to load executable/libraries directly.")); |
2473 | } | |
2474 | ||
2475 | hm = LoadLibrary ("advapi32.dll"); | |
2476 | if (hm) | |
2477 | { | |
2478 | dyn_OpenProcessToken = (void *) | |
2479 | GetProcAddress (hm, "OpenProcessToken"); | |
2480 | dyn_LookupPrivilegeValueA = (void *) | |
2481 | GetProcAddress (hm, "LookupPrivilegeValueA"); | |
2482 | dyn_AdjustTokenPrivileges = (void *) | |
2483 | GetProcAddress (hm, "AdjustTokenPrivileges"); | |
2484 | /* Only need to set one of these since if OpenProcessToken fails nothing | |
2485 | else is needed. */ | |
2486 | if (!dyn_OpenProcessToken || !dyn_LookupPrivilegeValueA || !dyn_AdjustTokenPrivileges) | |
2487 | dyn_OpenProcessToken = bad_OpenProcessToken; | |
2b008701 | 2488 | } |
33605d39 | 2489 | } |