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