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