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