Commit | Line | Data |
---|---|---|
b80864fb | 1 | /* Low level interface to Windows debugging, for gdbserver. |
b811d2c2 | 2 | Copyright (C) 2006-2020 Free Software Foundation, Inc. |
b80864fb DJ |
3 | |
4 | Contributed by Leo Zayas. Based on "win32-nat.c" from GDB. | |
5 | ||
6 | This file is part of GDB. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 10 | the Free Software Foundation; either version 3 of the License, or |
b80864fb DJ |
11 | (at your option) any later version. |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
b80864fb DJ |
20 | |
21 | #include "server.h" | |
22 | #include "regcache.h" | |
59a016f0 | 23 | #include "gdb/fileio.h" |
ed50f18f PA |
24 | #include "mem-break.h" |
25 | #include "win32-low.h" | |
623b6bdf | 26 | #include "gdbthread.h" |
799cdc37 | 27 | #include "dll.h" |
533b0600 | 28 | #include "hostio.h" |
b80864fb | 29 | #include <windows.h> |
ed50f18f | 30 | #include <winnt.h> |
b80864fb | 31 | #include <imagehlp.h> |
255e7678 | 32 | #include <tlhelp32.h> |
b80864fb | 33 | #include <psapi.h> |
b80864fb | 34 | #include <process.h> |
268a13a5 TT |
35 | #include "gdbsupport/gdb_tilde_expand.h" |
36 | #include "gdbsupport/common-inferior.h" | |
559e7e50 | 37 | #include "gdbsupport/gdb_wait.h" |
b80864fb | 38 | |
4834dad0 TT |
39 | using namespace windows_nat; |
40 | ||
b80864fb DJ |
41 | #ifndef USE_WIN32API |
42 | #include <sys/cygwin.h> | |
43 | #endif | |
44 | ||
10357975 PA |
45 | #define OUTMSG(X) do { printf X; fflush (stderr); } while (0) |
46 | ||
47 | #define OUTMSG2(X) \ | |
48 | do \ | |
49 | { \ | |
50 | if (debug_threads) \ | |
51 | { \ | |
52 | printf X; \ | |
53 | fflush (stderr); \ | |
54 | } \ | |
55 | } while (0) | |
ed50f18f PA |
56 | |
57 | #ifndef _T | |
58 | #define _T(x) TEXT (x) | |
59 | #endif | |
60 | ||
61 | #ifndef COUNTOF | |
62 | #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0])) | |
b80864fb DJ |
63 | #endif |
64 | ||
bf914831 PA |
65 | #ifdef _WIN32_WCE |
66 | # define GETPROCADDRESS(DLL, PROC) \ | |
67 | ((winapi_ ## PROC) GetProcAddress (DLL, TEXT (#PROC))) | |
68 | #else | |
69 | # define GETPROCADDRESS(DLL, PROC) \ | |
70 | ((winapi_ ## PROC) GetProcAddress (DLL, #PROC)) | |
71 | #endif | |
72 | ||
b80864fb DJ |
73 | int using_threads = 1; |
74 | ||
75 | /* Globals. */ | |
d97903b2 | 76 | static int attaching = 0; |
b80864fb DJ |
77 | static HANDLE current_process_handle = NULL; |
78 | static DWORD current_process_id = 0; | |
5ac588cf | 79 | static DWORD main_thread_id = 0; |
7928d571 | 80 | static EXCEPTION_RECORD siginfo_er; /* Contents of $_siginfo */ |
a493e3e2 | 81 | static enum gdb_signal last_sig = GDB_SIGNAL_0; |
b80864fb DJ |
82 | |
83 | /* The current debug event from WaitForDebugEvent. */ | |
84 | static DEBUG_EVENT current_event; | |
85 | ||
4210d83e PA |
86 | /* A status that hasn't been reported to the core yet, and so |
87 | win32_wait should return it next, instead of fetching the next | |
88 | debug event off the win32 API. */ | |
89 | static struct target_waitstatus cached_status; | |
90 | ||
4d5d1aaa PA |
91 | /* Non zero if an interrupt request is to be satisfied by suspending |
92 | all threads. */ | |
93 | static int soft_interrupt_requested = 0; | |
94 | ||
95 | /* Non zero if the inferior is stopped in a simulated breakpoint done | |
96 | by suspending all the threads. */ | |
97 | static int faked_breakpoint = 0; | |
98 | ||
3aee8918 PA |
99 | const struct target_desc *win32_tdesc; |
100 | ||
ed50f18f | 101 | #define NUM_REGS (the_low_target.num_regs) |
b80864fb | 102 | |
7a9a7487 MG |
103 | typedef BOOL (WINAPI *winapi_DebugActiveProcessStop) (DWORD dwProcessId); |
104 | typedef BOOL (WINAPI *winapi_DebugSetProcessKillOnExit) (BOOL KillOnExit); | |
105 | typedef BOOL (WINAPI *winapi_DebugBreakProcess) (HANDLE); | |
106 | typedef BOOL (WINAPI *winapi_GenerateConsoleCtrlEvent) (DWORD, DWORD); | |
b80864fb | 107 | |
379a5e2d | 108 | #ifndef _WIN32_WCE |
f25b3fc3 | 109 | static void win32_add_all_dlls (void); |
379a5e2d | 110 | #endif |
34b34921 | 111 | |
b80864fb DJ |
112 | /* Get the thread ID from the current selected inferior (the current |
113 | thread). */ | |
95954743 | 114 | static ptid_t |
0bfdf32f | 115 | current_thread_ptid (void) |
b80864fb | 116 | { |
80894984 | 117 | return current_ptid; |
95954743 PA |
118 | } |
119 | ||
120 | /* The current debug event from WaitForDebugEvent. */ | |
121 | static ptid_t | |
122 | debug_event_ptid (DEBUG_EVENT *event) | |
123 | { | |
fd79271b | 124 | return ptid_t (event->dwProcessId, event->dwThreadId, 0); |
b80864fb DJ |
125 | } |
126 | ||
9c6c8194 PA |
127 | /* Get the thread context of the thread associated with TH. */ |
128 | ||
129 | static void | |
e56f8ccb | 130 | win32_get_thread_context (windows_thread_info *th) |
9c6c8194 PA |
131 | { |
132 | memset (&th->context, 0, sizeof (CONTEXT)); | |
a2abc7de | 133 | (*the_low_target.get_thread_context) (th); |
9c6c8194 PA |
134 | #ifdef _WIN32_WCE |
135 | memcpy (&th->base_context, &th->context, sizeof (CONTEXT)); | |
136 | #endif | |
137 | } | |
138 | ||
139 | /* Set the thread context of the thread associated with TH. */ | |
140 | ||
141 | static void | |
e56f8ccb | 142 | win32_set_thread_context (windows_thread_info *th) |
9c6c8194 PA |
143 | { |
144 | #ifdef _WIN32_WCE | |
145 | /* Calling SuspendThread on a thread that is running kernel code | |
146 | will report that the suspending was successful, but in fact, that | |
147 | will often not be true. In those cases, the context returned by | |
148 | GetThreadContext will not be correct by the time the thread | |
149 | stops, hence we can't set that context back into the thread when | |
30baf67b | 150 | resuming - it will most likely crash the inferior. |
9c6c8194 PA |
151 | Unfortunately, there is no way to know when the thread will |
152 | really stop. To work around it, we'll only write the context | |
153 | back to the thread when either the user or GDB explicitly change | |
154 | it between stopping and resuming. */ | |
155 | if (memcmp (&th->context, &th->base_context, sizeof (CONTEXT)) != 0) | |
156 | #endif | |
a2abc7de | 157 | SetThreadContext (th->h, &th->context); |
9c6c8194 PA |
158 | } |
159 | ||
a2abc7de PA |
160 | /* Set the thread context of the thread associated with TH. */ |
161 | ||
162 | static void | |
e56f8ccb | 163 | win32_prepare_to_resume (windows_thread_info *th) |
b80864fb | 164 | { |
a2abc7de PA |
165 | if (the_low_target.prepare_to_resume != NULL) |
166 | (*the_low_target.prepare_to_resume) (th); | |
167 | } | |
b80864fb | 168 | |
a2abc7de | 169 | /* See win32-low.h. */ |
b80864fb | 170 | |
a2abc7de | 171 | void |
e56f8ccb | 172 | win32_require_context (windows_thread_info *th) |
a2abc7de PA |
173 | { |
174 | if (th->context.ContextFlags == 0) | |
b80864fb | 175 | { |
98a03287 | 176 | th->suspend (); |
9c6c8194 | 177 | win32_get_thread_context (th); |
b80864fb | 178 | } |
a2abc7de PA |
179 | } |
180 | ||
28688adf TT |
181 | /* See nat/windows-nat.h. */ |
182 | ||
183 | windows_thread_info * | |
184 | windows_nat::thread_rec (ptid_t ptid, thread_disposition_type disposition) | |
a2abc7de | 185 | { |
8dc7b443 | 186 | thread_info *thread = find_thread_ptid (ptid); |
a2abc7de PA |
187 | if (thread == NULL) |
188 | return NULL; | |
189 | ||
e56f8ccb | 190 | windows_thread_info *th = (windows_thread_info *) thread_target_data (thread); |
28688adf | 191 | if (disposition != DONT_INVALIDATE_CONTEXT) |
a2abc7de | 192 | win32_require_context (th); |
b80864fb DJ |
193 | return th; |
194 | } | |
195 | ||
196 | /* Add a thread to the thread list. */ | |
e56f8ccb | 197 | static windows_thread_info * |
711e434b | 198 | child_add_thread (DWORD pid, DWORD tid, HANDLE h, void *tlb) |
b80864fb | 199 | { |
e56f8ccb | 200 | windows_thread_info *th; |
fd79271b | 201 | ptid_t ptid = ptid_t (pid, tid, 0); |
b80864fb | 202 | |
28688adf | 203 | if ((th = thread_rec (ptid, DONT_INVALIDATE_CONTEXT))) |
b80864fb DJ |
204 | return th; |
205 | ||
e9534bd2 | 206 | th = new windows_thread_info (tid, h, (CORE_ADDR) (uintptr_t) tlb); |
b80864fb | 207 | |
95954743 | 208 | add_thread (ptid, th); |
b80864fb | 209 | |
34b34921 PA |
210 | if (the_low_target.thread_added != NULL) |
211 | (*the_low_target.thread_added) (th); | |
b80864fb DJ |
212 | |
213 | return th; | |
214 | } | |
215 | ||
216 | /* Delete a thread from the list of threads. */ | |
217 | static void | |
9c80ecd6 | 218 | delete_thread_info (thread_info *thread) |
b80864fb | 219 | { |
e56f8ccb | 220 | windows_thread_info *th = (windows_thread_info *) thread_target_data (thread); |
b80864fb | 221 | |
c3de4d92 | 222 | remove_thread (thread); |
e9534bd2 | 223 | delete th; |
b80864fb DJ |
224 | } |
225 | ||
226 | /* Delete a thread from the list of threads. */ | |
227 | static void | |
95954743 | 228 | child_delete_thread (DWORD pid, DWORD tid) |
b80864fb | 229 | { |
b80864fb | 230 | /* If the last thread is exiting, just return. */ |
9c80ecd6 | 231 | if (all_threads.size () == 1) |
b80864fb DJ |
232 | return; |
233 | ||
8dc7b443 | 234 | thread_info *thread = find_thread_ptid (ptid_t (pid, tid)); |
b80864fb DJ |
235 | if (thread == NULL) |
236 | return; | |
237 | ||
238 | delete_thread_info (thread); | |
239 | } | |
240 | ||
aa5ca48f DE |
241 | /* These watchpoint related wrapper functions simply pass on the function call |
242 | if the low target has registered a corresponding function. */ | |
243 | ||
a2b2297a TBA |
244 | bool |
245 | win32_process_target::supports_z_point_type (char z_type) | |
802e8e6d PA |
246 | { |
247 | return (the_low_target.supports_z_point_type != NULL | |
248 | && the_low_target.supports_z_point_type (z_type)); | |
249 | } | |
250 | ||
7e0bde70 TBA |
251 | int |
252 | win32_process_target::insert_point (enum raw_bkpt_type type, CORE_ADDR addr, | |
253 | int size, raw_breakpoint *bp) | |
aa5ca48f DE |
254 | { |
255 | if (the_low_target.insert_point != NULL) | |
802e8e6d | 256 | return the_low_target.insert_point (type, addr, size, bp); |
aa5ca48f DE |
257 | else |
258 | /* Unsupported (see target.h). */ | |
259 | return 1; | |
260 | } | |
261 | ||
7e0bde70 TBA |
262 | int |
263 | win32_process_target::remove_point (enum raw_bkpt_type type, CORE_ADDR addr, | |
264 | int size, raw_breakpoint *bp) | |
aa5ca48f DE |
265 | { |
266 | if (the_low_target.remove_point != NULL) | |
802e8e6d | 267 | return the_low_target.remove_point (type, addr, size, bp); |
aa5ca48f DE |
268 | else |
269 | /* Unsupported (see target.h). */ | |
270 | return 1; | |
271 | } | |
272 | ||
6eeb5c55 TBA |
273 | bool |
274 | win32_process_target::stopped_by_watchpoint () | |
aa5ca48f DE |
275 | { |
276 | if (the_low_target.stopped_by_watchpoint != NULL) | |
277 | return the_low_target.stopped_by_watchpoint (); | |
278 | else | |
6eeb5c55 | 279 | return false; |
aa5ca48f DE |
280 | } |
281 | ||
6eeb5c55 TBA |
282 | CORE_ADDR |
283 | win32_process_target::stopped_data_address () | |
aa5ca48f DE |
284 | { |
285 | if (the_low_target.stopped_data_address != NULL) | |
286 | return the_low_target.stopped_data_address (); | |
287 | else | |
288 | return 0; | |
289 | } | |
290 | ||
291 | ||
b80864fb DJ |
292 | /* Transfer memory from/to the debugged process. */ |
293 | static int | |
294 | child_xfer_memory (CORE_ADDR memaddr, char *our, int len, | |
5b6d1e4f | 295 | int write, process_stratum_target *target) |
b80864fb | 296 | { |
cee83bcb PM |
297 | BOOL success; |
298 | SIZE_T done = 0; | |
299 | DWORD lasterror = 0; | |
e8f0053d | 300 | uintptr_t addr = (uintptr_t) memaddr; |
b80864fb DJ |
301 | |
302 | if (write) | |
303 | { | |
cee83bcb PM |
304 | success = WriteProcessMemory (current_process_handle, (LPVOID) addr, |
305 | (LPCVOID) our, len, &done); | |
306 | if (!success) | |
307 | lasterror = GetLastError (); | |
b80864fb DJ |
308 | FlushInstructionCache (current_process_handle, (LPCVOID) addr, len); |
309 | } | |
310 | else | |
311 | { | |
cee83bcb PM |
312 | success = ReadProcessMemory (current_process_handle, (LPCVOID) addr, |
313 | (LPVOID) our, len, &done); | |
314 | if (!success) | |
315 | lasterror = GetLastError (); | |
b80864fb | 316 | } |
cee83bcb PM |
317 | if (!success && lasterror == ERROR_PARTIAL_COPY && done > 0) |
318 | return done; | |
319 | else | |
320 | return success ? done : -1; | |
b80864fb DJ |
321 | } |
322 | ||
ed50f18f | 323 | /* Clear out any old thread list and reinitialize it to a pristine |
b80864fb DJ |
324 | state. */ |
325 | static void | |
326 | child_init_thread_list (void) | |
327 | { | |
f0045347 | 328 | for_each_thread (delete_thread_info); |
b80864fb DJ |
329 | } |
330 | ||
f25b3fc3 JB |
331 | /* Zero during the child initialization phase, and nonzero otherwise. */ |
332 | ||
333 | static int child_initialization_done = 0; | |
334 | ||
b80864fb | 335 | static void |
95954743 | 336 | do_initial_child_stuff (HANDLE proch, DWORD pid, int attached) |
b80864fb | 337 | { |
3aee8918 PA |
338 | struct process_info *proc; |
339 | ||
a493e3e2 | 340 | last_sig = GDB_SIGNAL_0; |
b80864fb | 341 | |
5ac588cf PA |
342 | current_process_handle = proch; |
343 | current_process_id = pid; | |
344 | main_thread_id = 0; | |
345 | ||
346 | soft_interrupt_requested = 0; | |
347 | faked_breakpoint = 0; | |
348 | ||
b80864fb DJ |
349 | memset (¤t_event, 0, sizeof (current_event)); |
350 | ||
3aee8918 PA |
351 | proc = add_process (pid, attached); |
352 | proc->tdesc = win32_tdesc; | |
b80864fb | 353 | child_init_thread_list (); |
f25b3fc3 | 354 | child_initialization_done = 0; |
ed50f18f PA |
355 | |
356 | if (the_low_target.initial_stuff != NULL) | |
357 | (*the_low_target.initial_stuff) (); | |
4210d83e PA |
358 | |
359 | cached_status.kind = TARGET_WAITKIND_IGNORE; | |
360 | ||
361 | /* Flush all currently pending debug events (thread and dll list) up | |
362 | to the initial breakpoint. */ | |
363 | while (1) | |
364 | { | |
365 | struct target_waitstatus status; | |
366 | ||
52405d85 | 367 | the_target->wait (minus_one_ptid, &status, 0); |
4210d83e PA |
368 | |
369 | /* Note win32_wait doesn't return thread events. */ | |
370 | if (status.kind != TARGET_WAITKIND_LOADED) | |
371 | { | |
372 | cached_status = status; | |
373 | break; | |
374 | } | |
375 | ||
376 | { | |
377 | struct thread_resume resume; | |
378 | ||
379 | resume.thread = minus_one_ptid; | |
380 | resume.kind = resume_continue; | |
381 | resume.sig = 0; | |
382 | ||
52405d85 | 383 | the_target->resume (&resume, 1); |
4210d83e PA |
384 | } |
385 | } | |
379a5e2d JB |
386 | |
387 | #ifndef _WIN32_WCE | |
f25b3fc3 JB |
388 | /* Now that the inferior has been started and all DLLs have been mapped, |
389 | we can iterate over all DLLs and load them in. | |
390 | ||
391 | We avoid doing it any earlier because, on certain versions of Windows, | |
392 | LOAD_DLL_DEBUG_EVENTs are sometimes not complete. In particular, | |
393 | we have seen on Windows 8.1 that the ntdll.dll load event does not | |
394 | include the DLL name, preventing us from creating an associated SO. | |
395 | A possible explanation is that ntdll.dll might be mapped before | |
396 | the SO info gets created by the Windows system -- ntdll.dll is | |
397 | the first DLL to be reported via LOAD_DLL_DEBUG_EVENT and other DLLs | |
398 | do not seem to suffer from that problem. | |
399 | ||
400 | Rather than try to work around this sort of issue, it is much | |
401 | simpler to just ignore DLL load/unload events during the startup | |
402 | phase, and then process them all in one batch now. */ | |
403 | win32_add_all_dlls (); | |
379a5e2d | 404 | #endif |
f25b3fc3 JB |
405 | |
406 | child_initialization_done = 1; | |
b80864fb DJ |
407 | } |
408 | ||
409 | /* Resume all artificially suspended threads if we are continuing | |
410 | execution. */ | |
2bee2b6c SM |
411 | static void |
412 | continue_one_thread (thread_info *thread, int thread_id) | |
b80864fb | 413 | { |
e56f8ccb | 414 | windows_thread_info *th = (windows_thread_info *) thread_target_data (thread); |
b80864fb | 415 | |
a2abc7de | 416 | if (thread_id == -1 || thread_id == th->tid) |
b80864fb | 417 | { |
a2abc7de | 418 | win32_prepare_to_resume (th); |
34b34921 | 419 | |
a2abc7de | 420 | if (th->suspended) |
c436e841 | 421 | { |
a2abc7de PA |
422 | if (th->context.ContextFlags) |
423 | { | |
424 | win32_set_thread_context (th); | |
425 | th->context.ContextFlags = 0; | |
426 | } | |
427 | ||
98a03287 | 428 | th->resume (); |
c436e841 | 429 | } |
b80864fb | 430 | } |
b80864fb DJ |
431 | } |
432 | ||
433 | static BOOL | |
434 | child_continue (DWORD continue_status, int thread_id) | |
435 | { | |
4d5d1aaa PA |
436 | /* The inferior will only continue after the ContinueDebugEvent |
437 | call. */ | |
2bee2b6c SM |
438 | for_each_thread ([&] (thread_info *thread) |
439 | { | |
440 | continue_one_thread (thread, thread_id); | |
441 | }); | |
4d5d1aaa | 442 | faked_breakpoint = 0; |
b80864fb | 443 | |
4d5d1aaa PA |
444 | if (!ContinueDebugEvent (current_event.dwProcessId, |
445 | current_event.dwThreadId, | |
446 | continue_status)) | |
447 | return FALSE; | |
b80864fb | 448 | |
4d5d1aaa | 449 | return TRUE; |
b80864fb DJ |
450 | } |
451 | ||
b80864fb DJ |
452 | /* Fetch register(s) from the current thread context. */ |
453 | static void | |
442ea881 | 454 | child_fetch_inferior_registers (struct regcache *regcache, int r) |
b80864fb DJ |
455 | { |
456 | int regno; | |
28688adf TT |
457 | windows_thread_info *th = thread_rec (current_thread_ptid (), |
458 | INVALIDATE_CONTEXT); | |
4463ce24 | 459 | if (r == -1 || r > NUM_REGS) |
442ea881 | 460 | child_fetch_inferior_registers (regcache, NUM_REGS); |
b80864fb DJ |
461 | else |
462 | for (regno = 0; regno < r; regno++) | |
442ea881 | 463 | (*the_low_target.fetch_inferior_register) (regcache, th, regno); |
b80864fb DJ |
464 | } |
465 | ||
466 | /* Store a new register value into the current thread context. We don't | |
467 | change the program's context until later, when we resume it. */ | |
468 | static void | |
442ea881 | 469 | child_store_inferior_registers (struct regcache *regcache, int r) |
b80864fb DJ |
470 | { |
471 | int regno; | |
28688adf TT |
472 | windows_thread_info *th = thread_rec (current_thread_ptid (), |
473 | INVALIDATE_CONTEXT); | |
b80864fb | 474 | if (r == -1 || r == 0 || r > NUM_REGS) |
442ea881 | 475 | child_store_inferior_registers (regcache, NUM_REGS); |
b80864fb DJ |
476 | else |
477 | for (regno = 0; regno < r; regno++) | |
442ea881 | 478 | (*the_low_target.store_inferior_register) (regcache, th, regno); |
b80864fb DJ |
479 | } |
480 | ||
ed50f18f PA |
481 | /* Map the Windows error number in ERROR to a locale-dependent error |
482 | message string and return a pointer to it. Typically, the values | |
483 | for ERROR come from GetLastError. | |
484 | ||
485 | The string pointed to shall not be modified by the application, | |
486 | but may be overwritten by a subsequent call to strwinerror | |
487 | ||
488 | The strwinerror function does not change the current setting | |
489 | of GetLastError. */ | |
490 | ||
491 | char * | |
492 | strwinerror (DWORD error) | |
493 | { | |
494 | static char buf[1024]; | |
495 | TCHAR *msgbuf; | |
496 | DWORD lasterr = GetLastError (); | |
497 | DWORD chars = FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | |
498 | | FORMAT_MESSAGE_ALLOCATE_BUFFER, | |
499 | NULL, | |
500 | error, | |
501 | 0, /* Default language */ | |
c3de4d92 | 502 | (LPTSTR) &msgbuf, |
ed50f18f PA |
503 | 0, |
504 | NULL); | |
505 | if (chars != 0) | |
506 | { | |
507 | /* If there is an \r\n appended, zap it. */ | |
508 | if (chars >= 2 | |
509 | && msgbuf[chars - 2] == '\r' | |
510 | && msgbuf[chars - 1] == '\n') | |
511 | { | |
512 | chars -= 2; | |
513 | msgbuf[chars] = 0; | |
514 | } | |
515 | ||
516 | if (chars > ((COUNTOF (buf)) - 1)) | |
517 | { | |
518 | chars = COUNTOF (buf) - 1; | |
519 | msgbuf [chars] = 0; | |
520 | } | |
521 | ||
522 | #ifdef UNICODE | |
523 | wcstombs (buf, msgbuf, chars + 1); | |
524 | #else | |
525 | strncpy (buf, msgbuf, chars + 1); | |
526 | #endif | |
527 | LocalFree (msgbuf); | |
528 | } | |
529 | else | |
dfe07582 | 530 | sprintf (buf, "unknown win32 error (%u)", (unsigned) error); |
ed50f18f PA |
531 | |
532 | SetLastError (lasterr); | |
533 | return buf; | |
534 | } | |
535 | ||
aec18585 PA |
536 | static BOOL |
537 | create_process (const char *program, char *args, | |
538 | DWORD flags, PROCESS_INFORMATION *pi) | |
539 | { | |
bc3b087d | 540 | const char *inferior_cwd = get_inferior_cwd (); |
aec18585 | 541 | BOOL ret; |
a9b34532 EZ |
542 | size_t argslen, proglen; |
543 | ||
544 | proglen = strlen (program) + 1; | |
545 | argslen = strlen (args) + proglen; | |
aec18585 PA |
546 | |
547 | #ifdef _WIN32_WCE | |
bc3b087d | 548 | wchar_t *p, *wprogram, *wargs, *wcwd = NULL; |
aec18585 | 549 | |
a9b34532 EZ |
550 | wprogram = (wchar_t *) alloca (proglen * sizeof (wchar_t)); |
551 | mbstowcs (wprogram, program, proglen); | |
aec18585 PA |
552 | |
553 | for (p = wprogram; *p; ++p) | |
554 | if (L'/' == *p) | |
555 | *p = L'\\'; | |
556 | ||
aec18585 | 557 | wargs = alloca ((argslen + 1) * sizeof (wchar_t)); |
a9b34532 EZ |
558 | wcscpy (wargs, wprogram); |
559 | wcscat (wargs, L" "); | |
560 | mbstowcs (wargs + proglen, args, argslen + 1 - proglen); | |
aec18585 | 561 | |
bc3b087d SDJ |
562 | if (inferior_cwd != NULL) |
563 | { | |
906994d9 | 564 | std::string expanded_infcwd = gdb_tilde_expand (inferior_cwd); |
bc3b087d SDJ |
565 | std::replace (expanded_infcwd.begin (), expanded_infcwd.end (), |
566 | '/', '\\'); | |
567 | wcwd = alloca ((expanded_infcwd.size () + 1) * sizeof (wchar_t)); | |
568 | if (mbstowcs (wcwd, expanded_infcwd.c_str (), | |
569 | expanded_infcwd.size () + 1) == NULL) | |
570 | { | |
571 | error (_("\ | |
572 | Could not convert the expanded inferior cwd to wide-char.")); | |
573 | } | |
574 | } | |
575 | ||
aec18585 | 576 | ret = CreateProcessW (wprogram, /* image name */ |
1b3f6016 PA |
577 | wargs, /* command line */ |
578 | NULL, /* security, not supported */ | |
579 | NULL, /* thread, not supported */ | |
580 | FALSE, /* inherit handles, not supported */ | |
581 | flags, /* start flags */ | |
582 | NULL, /* environment, not supported */ | |
bc3b087d | 583 | wcwd, /* current directory */ |
1b3f6016 PA |
584 | NULL, /* start info, not supported */ |
585 | pi); /* proc info */ | |
aec18585 PA |
586 | #else |
587 | STARTUPINFOA si = { sizeof (STARTUPINFOA) }; | |
a9b34532 EZ |
588 | char *program_and_args = (char *) alloca (argslen + 1); |
589 | ||
590 | strcpy (program_and_args, program); | |
591 | strcat (program_and_args, " "); | |
592 | strcat (program_and_args, args); | |
593 | ret = CreateProcessA (program, /* image name */ | |
594 | program_and_args, /* command line */ | |
595 | NULL, /* security */ | |
596 | NULL, /* thread */ | |
597 | TRUE, /* inherit handles */ | |
598 | flags, /* start flags */ | |
599 | NULL, /* environment */ | |
906994d9 JB |
600 | /* current directory */ |
601 | (inferior_cwd == NULL | |
602 | ? NULL | |
603 | : gdb_tilde_expand (inferior_cwd).c_str()), | |
a9b34532 EZ |
604 | &si, /* start info */ |
605 | pi); /* proc info */ | |
aec18585 PA |
606 | #endif |
607 | ||
608 | return ret; | |
609 | } | |
610 | ||
b80864fb | 611 | /* Start a new process. |
2090129c SDJ |
612 | PROGRAM is the program name. |
613 | PROGRAM_ARGS is the vector containing the inferior's args. | |
b80864fb DJ |
614 | Returns the new PID on success, -1 on failure. Registers the new |
615 | process with the process list. */ | |
15295543 TBA |
616 | int |
617 | win32_process_target::create_inferior (const char *program, | |
618 | const std::vector<char *> &program_args) | |
b80864fb | 619 | { |
6341380d | 620 | client_state &cs = get_client_state (); |
b80864fb | 621 | #ifndef USE_WIN32API |
d8d2a3ee | 622 | char real_path[PATH_MAX]; |
b80864fb DJ |
623 | char *orig_path, *new_path, *path_ptr; |
624 | #endif | |
b80864fb DJ |
625 | BOOL ret; |
626 | DWORD flags; | |
ed50f18f | 627 | PROCESS_INFORMATION pi; |
aec18585 | 628 | DWORD err; |
2090129c SDJ |
629 | std::string str_program_args = stringify_argv (program_args); |
630 | char *args = (char *) str_program_args.c_str (); | |
b80864fb | 631 | |
d97903b2 PA |
632 | /* win32_wait needs to know we're not attaching. */ |
633 | attaching = 0; | |
634 | ||
b80864fb DJ |
635 | if (!program) |
636 | error ("No executable specified, specify executable to debug.\n"); | |
637 | ||
b80864fb DJ |
638 | flags = DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS; |
639 | ||
640 | #ifndef USE_WIN32API | |
641 | orig_path = NULL; | |
642 | path_ptr = getenv ("PATH"); | |
643 | if (path_ptr) | |
644 | { | |
81239425 | 645 | int size = cygwin_conv_path_list (CCP_POSIX_TO_WIN_A, path_ptr, NULL, 0); |
0ae534d2 JT |
646 | orig_path = (char *) alloca (strlen (path_ptr) + 1); |
647 | new_path = (char *) alloca (size); | |
b80864fb | 648 | strcpy (orig_path, path_ptr); |
81239425 | 649 | cygwin_conv_path_list (CCP_POSIX_TO_WIN_A, path_ptr, new_path, size); |
b80864fb | 650 | setenv ("PATH", new_path, 1); |
81239425 | 651 | } |
d8d2a3ee | 652 | cygwin_conv_path (CCP_POSIX_TO_WIN_A, program, real_path, PATH_MAX); |
b80864fb DJ |
653 | program = real_path; |
654 | #endif | |
655 | ||
a9b34532 | 656 | OUTMSG2 (("Command line is \"%s %s\"\n", program, args)); |
b80864fb | 657 | |
ed50f18f | 658 | #ifdef CREATE_NEW_PROCESS_GROUP |
b80864fb | 659 | flags |= CREATE_NEW_PROCESS_GROUP; |
ed50f18f | 660 | #endif |
b80864fb | 661 | |
aec18585 PA |
662 | ret = create_process (program, args, flags, &pi); |
663 | err = GetLastError (); | |
664 | if (!ret && err == ERROR_FILE_NOT_FOUND) | |
665 | { | |
c3de4d92 | 666 | char *exename = (char *) alloca (strlen (program) + 5); |
aec18585 PA |
667 | strcat (strcpy (exename, program), ".exe"); |
668 | ret = create_process (exename, args, flags, &pi); | |
669 | err = GetLastError (); | |
670 | } | |
b80864fb DJ |
671 | |
672 | #ifndef USE_WIN32API | |
673 | if (orig_path) | |
674 | setenv ("PATH", orig_path, 1); | |
675 | #endif | |
676 | ||
677 | if (!ret) | |
678 | { | |
a9b34532 | 679 | error ("Error creating process \"%s %s\", (error %d): %s\n", |
ed50f18f | 680 | program, args, (int) err, strwinerror (err)); |
b80864fb DJ |
681 | } |
682 | else | |
683 | { | |
a9b34532 | 684 | OUTMSG2 (("Process created: %s %s\n", program, (char *) args)); |
b80864fb DJ |
685 | } |
686 | ||
ed50f18f PA |
687 | #ifndef _WIN32_WCE |
688 | /* On Windows CE this handle can't be closed. The OS reuses | |
689 | it in the debug events, while the 9x/NT versions of Windows | |
690 | probably use a DuplicateHandle'd one. */ | |
b80864fb | 691 | CloseHandle (pi.hThread); |
ed50f18f | 692 | #endif |
b80864fb | 693 | |
95954743 | 694 | do_initial_child_stuff (pi.hProcess, pi.dwProcessId, 0); |
b80864fb | 695 | |
7dbac825 JB |
696 | /* Wait till we are at 1st instruction in program, return new pid |
697 | (assuming success). */ | |
6532e7e3 | 698 | cs.last_ptid = wait (ptid_t (current_process_id), &cs.last_status, 0); |
7dbac825 | 699 | |
052793ad HD |
700 | /* Necessary for handle_v_kill. */ |
701 | signal_pid = current_process_id; | |
702 | ||
b80864fb DJ |
703 | return current_process_id; |
704 | } | |
705 | ||
706 | /* Attach to a running process. | |
707 | PID is the process ID to attach to, specified by the user | |
708 | or a higher layer. */ | |
ef03dad8 TBA |
709 | int |
710 | win32_process_target::attach (unsigned long pid) | |
b80864fb | 711 | { |
5ca906e6 | 712 | HANDLE h; |
bf914831 | 713 | winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit = NULL; |
5ca906e6 | 714 | DWORD err; |
ed50f18f PA |
715 | #ifdef _WIN32_WCE |
716 | HMODULE dll = GetModuleHandle (_T("COREDLL.DLL")); | |
717 | #else | |
718 | HMODULE dll = GetModuleHandle (_T("KERNEL32.DLL")); | |
719 | #endif | |
bf914831 | 720 | DebugSetProcessKillOnExit = GETPROCADDRESS (dll, DebugSetProcessKillOnExit); |
b80864fb | 721 | |
5ca906e6 PA |
722 | h = OpenProcess (PROCESS_ALL_ACCESS, FALSE, pid); |
723 | if (h != NULL) | |
1d5315fe | 724 | { |
5ca906e6 PA |
725 | if (DebugActiveProcess (pid)) |
726 | { | |
727 | if (DebugSetProcessKillOnExit != NULL) | |
728 | DebugSetProcessKillOnExit (FALSE); | |
729 | ||
d97903b2 | 730 | /* win32_wait needs to know we're attaching. */ |
1b3f6016 | 731 | attaching = 1; |
95954743 | 732 | do_initial_child_stuff (h, pid, 1); |
5ca906e6 PA |
733 | return 0; |
734 | } | |
735 | ||
736 | CloseHandle (h); | |
b80864fb DJ |
737 | } |
738 | ||
5ca906e6 PA |
739 | err = GetLastError (); |
740 | error ("Attach to process failed (error %d): %s\n", | |
741 | (int) err, strwinerror (err)); | |
b80864fb DJ |
742 | } |
743 | ||
bce7165d PA |
744 | /* Handle OUTPUT_DEBUG_STRING_EVENT from child process. */ |
745 | static void | |
c0879059 | 746 | handle_output_debug_string (void) |
bce7165d PA |
747 | { |
748 | #define READ_BUFFER_LEN 1024 | |
749 | CORE_ADDR addr; | |
750 | char s[READ_BUFFER_LEN + 1] = { 0 }; | |
751 | DWORD nbytes = current_event.u.DebugString.nDebugStringLength; | |
752 | ||
753 | if (nbytes == 0) | |
754 | return; | |
755 | ||
756 | if (nbytes > READ_BUFFER_LEN) | |
757 | nbytes = READ_BUFFER_LEN; | |
758 | ||
759 | addr = (CORE_ADDR) (size_t) current_event.u.DebugString.lpDebugStringData; | |
760 | ||
761 | if (current_event.u.DebugString.fUnicode) | |
762 | { | |
763 | /* The event tells us how many bytes, not chars, even | |
1b3f6016 | 764 | in Unicode. */ |
bce7165d PA |
765 | WCHAR buffer[(READ_BUFFER_LEN + 1) / sizeof (WCHAR)] = { 0 }; |
766 | if (read_inferior_memory (addr, (unsigned char *) buffer, nbytes) != 0) | |
767 | return; | |
768 | wcstombs (s, buffer, (nbytes + 1) / sizeof (WCHAR)); | |
769 | } | |
770 | else | |
771 | { | |
772 | if (read_inferior_memory (addr, (unsigned char *) s, nbytes) != 0) | |
773 | return; | |
774 | } | |
775 | ||
61012eef | 776 | if (!startswith (s, "cYg")) |
45e2715e PA |
777 | { |
778 | if (!server_waiting) | |
779 | { | |
780 | OUTMSG2(("%s", s)); | |
781 | return; | |
782 | } | |
783 | ||
784 | monitor_output (s); | |
785 | } | |
bce7165d PA |
786 | #undef READ_BUFFER_LEN |
787 | } | |
788 | ||
5ac588cf PA |
789 | static void |
790 | win32_clear_inferiors (void) | |
791 | { | |
792 | if (current_process_handle != NULL) | |
793 | CloseHandle (current_process_handle); | |
794 | ||
f0045347 | 795 | for_each_thread (delete_thread_info); |
7928d571 | 796 | siginfo_er.ExceptionCode = 0; |
5ac588cf PA |
797 | clear_inferiors (); |
798 | } | |
799 | ||
a780ef4f PA |
800 | /* Implementation of target_ops::kill. */ |
801 | ||
c6885a57 TBA |
802 | int |
803 | win32_process_target::kill (process_info *process) | |
b80864fb DJ |
804 | { |
805 | TerminateProcess (current_process_handle, 0); | |
806 | for (;;) | |
807 | { | |
808 | if (!child_continue (DBG_CONTINUE, -1)) | |
809 | break; | |
810 | if (!WaitForDebugEvent (¤t_event, INFINITE)) | |
811 | break; | |
812 | if (current_event.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT) | |
813 | break; | |
bce7165d | 814 | else if (current_event.dwDebugEventCode == OUTPUT_DEBUG_STRING_EVENT) |
c0879059 | 815 | handle_output_debug_string (); |
b80864fb | 816 | } |
ed50f18f | 817 | |
5ac588cf | 818 | win32_clear_inferiors (); |
95954743 | 819 | |
95954743 PA |
820 | remove_process (process); |
821 | return 0; | |
b80864fb DJ |
822 | } |
823 | ||
ef2ddb33 PA |
824 | /* Implementation of target_ops::detach. */ |
825 | ||
9061c9cf TBA |
826 | int |
827 | win32_process_target::detach (process_info *process) | |
b80864fb | 828 | { |
bf914831 PA |
829 | winapi_DebugActiveProcessStop DebugActiveProcessStop = NULL; |
830 | winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit = NULL; | |
ed50f18f PA |
831 | #ifdef _WIN32_WCE |
832 | HMODULE dll = GetModuleHandle (_T("COREDLL.DLL")); | |
833 | #else | |
834 | HMODULE dll = GetModuleHandle (_T("KERNEL32.DLL")); | |
835 | #endif | |
bf914831 PA |
836 | DebugActiveProcessStop = GETPROCADDRESS (dll, DebugActiveProcessStop); |
837 | DebugSetProcessKillOnExit = GETPROCADDRESS (dll, DebugSetProcessKillOnExit); | |
b80864fb | 838 | |
444d6139 PA |
839 | if (DebugSetProcessKillOnExit == NULL |
840 | || DebugActiveProcessStop == NULL) | |
841 | return -1; | |
b80864fb | 842 | |
444d6139 PA |
843 | { |
844 | struct thread_resume resume; | |
95954743 | 845 | resume.thread = minus_one_ptid; |
bd99dc85 | 846 | resume.kind = resume_continue; |
444d6139 | 847 | resume.sig = 0; |
0e4d7e35 | 848 | this->resume (&resume, 1); |
444d6139 PA |
849 | } |
850 | ||
851 | if (!DebugActiveProcessStop (current_process_id)) | |
5ac588cf PA |
852 | return -1; |
853 | ||
444d6139 | 854 | DebugSetProcessKillOnExit (FALSE); |
95954743 | 855 | remove_process (process); |
444d6139 | 856 | |
5ac588cf | 857 | win32_clear_inferiors (); |
444d6139 PA |
858 | return 0; |
859 | } | |
860 | ||
8adb37b9 TBA |
861 | void |
862 | win32_process_target::mourn (struct process_info *process) | |
505106cd PA |
863 | { |
864 | remove_process (process); | |
865 | } | |
866 | ||
ef2ddb33 PA |
867 | /* Implementation of target_ops::join. */ |
868 | ||
95a49a39 TBA |
869 | void |
870 | win32_process_target::join (int pid) | |
444d6139 | 871 | { |
d105de22 | 872 | HANDLE h = OpenProcess (PROCESS_ALL_ACCESS, FALSE, pid); |
5ac588cf PA |
873 | if (h != NULL) |
874 | { | |
875 | WaitForSingleObject (h, INFINITE); | |
876 | CloseHandle (h); | |
877 | } | |
b80864fb DJ |
878 | } |
879 | ||
13d3d99b TBA |
880 | /* Return true iff the thread with thread ID TID is alive. */ |
881 | bool | |
882 | win32_process_target::thread_alive (ptid_t ptid) | |
b80864fb | 883 | { |
b80864fb DJ |
884 | /* Our thread list is reliable; don't bother to poll target |
885 | threads. */ | |
8dc7b443 | 886 | return find_thread_ptid (ptid) != NULL; |
b80864fb DJ |
887 | } |
888 | ||
889 | /* Resume the inferior process. RESUME_INFO describes how we want | |
890 | to resume. */ | |
0e4d7e35 TBA |
891 | void |
892 | win32_process_target::resume (thread_resume *resume_info, size_t n) | |
b80864fb DJ |
893 | { |
894 | DWORD tid; | |
2ea28649 | 895 | enum gdb_signal sig; |
b80864fb | 896 | int step; |
e56f8ccb | 897 | windows_thread_info *th; |
b80864fb | 898 | DWORD continue_status = DBG_CONTINUE; |
95954743 | 899 | ptid_t ptid; |
b80864fb DJ |
900 | |
901 | /* This handles the very limited set of resume packets that GDB can | |
902 | currently produce. */ | |
903 | ||
d7e15655 | 904 | if (n == 1 && resume_info[0].thread == minus_one_ptid) |
b80864fb | 905 | tid = -1; |
2bd7c093 | 906 | else if (n > 1) |
b80864fb DJ |
907 | tid = -1; |
908 | else | |
909 | /* Yes, we're ignoring resume_info[0].thread. It'd be tricky to make | |
910 | the Windows resume code do the right thing for thread switching. */ | |
911 | tid = current_event.dwThreadId; | |
912 | ||
d7e15655 | 913 | if (resume_info[0].thread != minus_one_ptid) |
b80864fb | 914 | { |
ce7715e2 | 915 | sig = gdb_signal_from_host (resume_info[0].sig); |
bd99dc85 | 916 | step = resume_info[0].kind == resume_step; |
b80864fb DJ |
917 | } |
918 | else | |
919 | { | |
ce7715e2 | 920 | sig = GDB_SIGNAL_0; |
b80864fb DJ |
921 | step = 0; |
922 | } | |
923 | ||
a493e3e2 | 924 | if (sig != GDB_SIGNAL_0) |
b80864fb DJ |
925 | { |
926 | if (current_event.dwDebugEventCode != EXCEPTION_DEBUG_EVENT) | |
927 | { | |
ce7715e2 PA |
928 | OUTMSG (("Cannot continue with signal %s here.\n", |
929 | gdb_signal_to_string (sig))); | |
b80864fb DJ |
930 | } |
931 | else if (sig == last_sig) | |
932 | continue_status = DBG_EXCEPTION_NOT_HANDLED; | |
933 | else | |
ce7715e2 PA |
934 | OUTMSG (("Can only continue with received signal %s.\n", |
935 | gdb_signal_to_string (last_sig))); | |
b80864fb DJ |
936 | } |
937 | ||
a493e3e2 | 938 | last_sig = GDB_SIGNAL_0; |
b80864fb DJ |
939 | |
940 | /* Get context for the currently selected thread. */ | |
95954743 | 941 | ptid = debug_event_ptid (¤t_event); |
28688adf | 942 | th = thread_rec (ptid, DONT_INVALIDATE_CONTEXT); |
b80864fb DJ |
943 | if (th) |
944 | { | |
a2abc7de PA |
945 | win32_prepare_to_resume (th); |
946 | ||
b80864fb DJ |
947 | if (th->context.ContextFlags) |
948 | { | |
b80864fb DJ |
949 | /* Move register values from the inferior into the thread |
950 | context structure. */ | |
951 | regcache_invalidate (); | |
952 | ||
953 | if (step) | |
ed50f18f PA |
954 | { |
955 | if (the_low_target.single_step != NULL) | |
956 | (*the_low_target.single_step) (th); | |
957 | else | |
958 | error ("Single stepping is not supported " | |
959 | "in this configuration.\n"); | |
960 | } | |
34b34921 | 961 | |
9c6c8194 | 962 | win32_set_thread_context (th); |
b80864fb DJ |
963 | th->context.ContextFlags = 0; |
964 | } | |
965 | } | |
966 | ||
967 | /* Allow continuing with the same signal that interrupted us. | |
968 | Otherwise complain. */ | |
969 | ||
970 | child_continue (continue_status, tid); | |
971 | } | |
972 | ||
255e7678 DJ |
973 | static void |
974 | win32_add_one_solib (const char *name, CORE_ADDR load_addr) | |
975 | { | |
976 | char buf[MAX_PATH + 1]; | |
977 | char buf2[MAX_PATH + 1]; | |
978 | ||
979 | #ifdef _WIN32_WCE | |
980 | WIN32_FIND_DATA w32_fd; | |
981 | WCHAR wname[MAX_PATH + 1]; | |
982 | mbstowcs (wname, name, MAX_PATH); | |
983 | HANDLE h = FindFirstFile (wname, &w32_fd); | |
984 | #else | |
985 | WIN32_FIND_DATAA w32_fd; | |
986 | HANDLE h = FindFirstFileA (name, &w32_fd); | |
987 | #endif | |
988 | ||
850a0f76 JB |
989 | /* The symbols in a dll are offset by 0x1000, which is the |
990 | offset from 0 of the first byte in an image - because | |
991 | of the file header and the section alignment. */ | |
992 | load_addr += 0x1000; | |
993 | ||
255e7678 DJ |
994 | if (h == INVALID_HANDLE_VALUE) |
995 | strcpy (buf, name); | |
996 | else | |
997 | { | |
998 | FindClose (h); | |
999 | strcpy (buf, name); | |
1000 | #ifndef _WIN32_WCE | |
1001 | { | |
1002 | char cwd[MAX_PATH + 1]; | |
1003 | char *p; | |
1004 | if (GetCurrentDirectoryA (MAX_PATH + 1, cwd)) | |
1005 | { | |
1006 | p = strrchr (buf, '\\'); | |
1007 | if (p) | |
1008 | p[1] = '\0'; | |
1009 | SetCurrentDirectoryA (buf); | |
1010 | GetFullPathNameA (w32_fd.cFileName, MAX_PATH, buf, &p); | |
1011 | SetCurrentDirectoryA (cwd); | |
1012 | } | |
1013 | } | |
1014 | #endif | |
1015 | } | |
1016 | ||
cf6e3471 PA |
1017 | #ifndef _WIN32_WCE |
1018 | if (strcasecmp (buf, "ntdll.dll") == 0) | |
1019 | { | |
1020 | GetSystemDirectoryA (buf, sizeof (buf)); | |
1021 | strcat (buf, "\\ntdll.dll"); | |
1022 | } | |
1023 | #endif | |
1024 | ||
255e7678 | 1025 | #ifdef __CYGWIN__ |
81239425 | 1026 | cygwin_conv_path (CCP_WIN_A_TO_POSIX, buf, buf2, sizeof (buf2)); |
255e7678 DJ |
1027 | #else |
1028 | strcpy (buf2, buf); | |
1029 | #endif | |
1030 | ||
1031 | loaded_dll (buf2, load_addr); | |
1032 | } | |
1033 | ||
255e7678 DJ |
1034 | typedef BOOL (WINAPI *winapi_EnumProcessModules) (HANDLE, HMODULE *, |
1035 | DWORD, LPDWORD); | |
1036 | typedef BOOL (WINAPI *winapi_GetModuleInformation) (HANDLE, HMODULE, | |
1037 | LPMODULEINFO, DWORD); | |
1038 | typedef DWORD (WINAPI *winapi_GetModuleFileNameExA) (HANDLE, HMODULE, | |
1039 | LPSTR, DWORD); | |
1040 | ||
1041 | static winapi_EnumProcessModules win32_EnumProcessModules; | |
1042 | static winapi_GetModuleInformation win32_GetModuleInformation; | |
1043 | static winapi_GetModuleFileNameExA win32_GetModuleFileNameExA; | |
1044 | ||
1045 | static BOOL | |
1046 | load_psapi (void) | |
1047 | { | |
1048 | static int psapi_loaded = 0; | |
1049 | static HMODULE dll = NULL; | |
1050 | ||
1051 | if (!psapi_loaded) | |
1052 | { | |
1053 | psapi_loaded = 1; | |
1054 | dll = LoadLibrary (TEXT("psapi.dll")); | |
1055 | if (!dll) | |
1056 | return FALSE; | |
1057 | win32_EnumProcessModules = | |
1058 | GETPROCADDRESS (dll, EnumProcessModules); | |
1059 | win32_GetModuleInformation = | |
1060 | GETPROCADDRESS (dll, GetModuleInformation); | |
1061 | win32_GetModuleFileNameExA = | |
1062 | GETPROCADDRESS (dll, GetModuleFileNameExA); | |
1063 | } | |
1064 | ||
1065 | return (win32_EnumProcessModules != NULL | |
1066 | && win32_GetModuleInformation != NULL | |
1067 | && win32_GetModuleFileNameExA != NULL); | |
1068 | } | |
1069 | ||
379a5e2d | 1070 | #ifndef _WIN32_WCE |
649ebbca | 1071 | |
f25b3fc3 JB |
1072 | /* Iterate over all DLLs currently mapped by our inferior, and |
1073 | add them to our list of solibs. */ | |
379a5e2d JB |
1074 | |
1075 | static void | |
f25b3fc3 | 1076 | win32_add_all_dlls (void) |
379a5e2d | 1077 | { |
379a5e2d JB |
1078 | size_t i; |
1079 | HMODULE dh_buf[1]; | |
1080 | HMODULE *DllHandle = dh_buf; | |
1081 | DWORD cbNeeded; | |
1082 | BOOL ok; | |
1083 | ||
379a5e2d JB |
1084 | if (!load_psapi ()) |
1085 | return; | |
1086 | ||
1087 | cbNeeded = 0; | |
1088 | ok = (*win32_EnumProcessModules) (current_process_handle, | |
1089 | DllHandle, | |
1090 | sizeof (HMODULE), | |
1091 | &cbNeeded); | |
1092 | ||
1093 | if (!ok || !cbNeeded) | |
1094 | return; | |
1095 | ||
1096 | DllHandle = (HMODULE *) alloca (cbNeeded); | |
1097 | if (!DllHandle) | |
1098 | return; | |
1099 | ||
1100 | ok = (*win32_EnumProcessModules) (current_process_handle, | |
1101 | DllHandle, | |
1102 | cbNeeded, | |
1103 | &cbNeeded); | |
1104 | if (!ok) | |
1105 | return; | |
1106 | ||
f25b3fc3 | 1107 | for (i = 1; i < ((size_t) cbNeeded / sizeof (HMODULE)); i++) |
379a5e2d JB |
1108 | { |
1109 | MODULEINFO mi; | |
1110 | char dll_name[MAX_PATH]; | |
1111 | ||
1112 | if (!(*win32_GetModuleInformation) (current_process_handle, | |
1113 | DllHandle[i], | |
1114 | &mi, | |
1115 | sizeof (mi))) | |
1116 | continue; | |
1117 | if ((*win32_GetModuleFileNameExA) (current_process_handle, | |
1118 | DllHandle[i], | |
1119 | dll_name, | |
1120 | MAX_PATH) == 0) | |
1121 | continue; | |
850a0f76 | 1122 | win32_add_one_solib (dll_name, (CORE_ADDR) (uintptr_t) mi.lpBaseOfDll); |
379a5e2d JB |
1123 | } |
1124 | } | |
1125 | #endif | |
1126 | ||
255e7678 DJ |
1127 | typedef HANDLE (WINAPI *winapi_CreateToolhelp32Snapshot) (DWORD, DWORD); |
1128 | typedef BOOL (WINAPI *winapi_Module32First) (HANDLE, LPMODULEENTRY32); | |
1129 | typedef BOOL (WINAPI *winapi_Module32Next) (HANDLE, LPMODULEENTRY32); | |
1130 | ||
d763de10 | 1131 | /* Handle a DLL load event. |
255e7678 | 1132 | |
d763de10 JB |
1133 | This function assumes that this event did not occur during inferior |
1134 | initialization, where their event info may be incomplete (see | |
1135 | do_initial_child_stuff and win32_add_all_dlls for more info on | |
1136 | how we handle DLL loading during that phase). */ | |
255e7678 DJ |
1137 | |
1138 | static void | |
1139 | handle_load_dll (void) | |
1140 | { | |
1141 | LOAD_DLL_DEBUG_INFO *event = ¤t_event.u.LoadDll; | |
9d8679cc | 1142 | const char *dll_name; |
255e7678 | 1143 | |
d763de10 JB |
1144 | dll_name = get_image_name (current_process_handle, |
1145 | event->lpImageName, event->fUnicode); | |
255e7678 DJ |
1146 | if (!dll_name) |
1147 | return; | |
1148 | ||
850a0f76 | 1149 | win32_add_one_solib (dll_name, (CORE_ADDR) (uintptr_t) event->lpBaseOfDll); |
255e7678 DJ |
1150 | } |
1151 | ||
f25b3fc3 JB |
1152 | /* Handle a DLL unload event. |
1153 | ||
1154 | This function assumes that this event did not occur during inferior | |
1155 | initialization, where their event info may be incomplete (see | |
1156 | do_initial_child_stuff and win32_add_one_solib for more info | |
1157 | on how we handle DLL loading during that phase). */ | |
1158 | ||
255e7678 DJ |
1159 | static void |
1160 | handle_unload_dll (void) | |
1161 | { | |
1162 | CORE_ADDR load_addr = | |
e8f0053d | 1163 | (CORE_ADDR) (uintptr_t) current_event.u.UnloadDll.lpBaseOfDll; |
850a0f76 JB |
1164 | |
1165 | /* The symbols in a dll are offset by 0x1000, which is the | |
1166 | offset from 0 of the first byte in an image - because | |
1167 | of the file header and the section alignment. */ | |
255e7678 DJ |
1168 | load_addr += 0x1000; |
1169 | unloaded_dll (NULL, load_addr); | |
1170 | } | |
1171 | ||
34b34921 | 1172 | static void |
b80864fb DJ |
1173 | handle_exception (struct target_waitstatus *ourstatus) |
1174 | { | |
b80864fb DJ |
1175 | DWORD code = current_event.u.Exception.ExceptionRecord.ExceptionCode; |
1176 | ||
7928d571 HD |
1177 | memcpy (&siginfo_er, ¤t_event.u.Exception.ExceptionRecord, |
1178 | sizeof siginfo_er); | |
1179 | ||
b80864fb DJ |
1180 | ourstatus->kind = TARGET_WAITKIND_STOPPED; |
1181 | ||
b80864fb DJ |
1182 | switch (code) |
1183 | { | |
1184 | case EXCEPTION_ACCESS_VIOLATION: | |
1185 | OUTMSG2 (("EXCEPTION_ACCESS_VIOLATION")); | |
a493e3e2 | 1186 | ourstatus->value.sig = GDB_SIGNAL_SEGV; |
b80864fb DJ |
1187 | break; |
1188 | case STATUS_STACK_OVERFLOW: | |
1189 | OUTMSG2 (("STATUS_STACK_OVERFLOW")); | |
a493e3e2 | 1190 | ourstatus->value.sig = GDB_SIGNAL_SEGV; |
b80864fb DJ |
1191 | break; |
1192 | case STATUS_FLOAT_DENORMAL_OPERAND: | |
1193 | OUTMSG2 (("STATUS_FLOAT_DENORMAL_OPERAND")); | |
a493e3e2 | 1194 | ourstatus->value.sig = GDB_SIGNAL_FPE; |
b80864fb DJ |
1195 | break; |
1196 | case EXCEPTION_ARRAY_BOUNDS_EXCEEDED: | |
1197 | OUTMSG2 (("EXCEPTION_ARRAY_BOUNDS_EXCEEDED")); | |
a493e3e2 | 1198 | ourstatus->value.sig = GDB_SIGNAL_FPE; |
b80864fb DJ |
1199 | break; |
1200 | case STATUS_FLOAT_INEXACT_RESULT: | |
1201 | OUTMSG2 (("STATUS_FLOAT_INEXACT_RESULT")); | |
a493e3e2 | 1202 | ourstatus->value.sig = GDB_SIGNAL_FPE; |
b80864fb DJ |
1203 | break; |
1204 | case STATUS_FLOAT_INVALID_OPERATION: | |
1205 | OUTMSG2 (("STATUS_FLOAT_INVALID_OPERATION")); | |
a493e3e2 | 1206 | ourstatus->value.sig = GDB_SIGNAL_FPE; |
b80864fb DJ |
1207 | break; |
1208 | case STATUS_FLOAT_OVERFLOW: | |
1209 | OUTMSG2 (("STATUS_FLOAT_OVERFLOW")); | |
a493e3e2 | 1210 | ourstatus->value.sig = GDB_SIGNAL_FPE; |
b80864fb DJ |
1211 | break; |
1212 | case STATUS_FLOAT_STACK_CHECK: | |
1213 | OUTMSG2 (("STATUS_FLOAT_STACK_CHECK")); | |
a493e3e2 | 1214 | ourstatus->value.sig = GDB_SIGNAL_FPE; |
b80864fb DJ |
1215 | break; |
1216 | case STATUS_FLOAT_UNDERFLOW: | |
1217 | OUTMSG2 (("STATUS_FLOAT_UNDERFLOW")); | |
a493e3e2 | 1218 | ourstatus->value.sig = GDB_SIGNAL_FPE; |
b80864fb DJ |
1219 | break; |
1220 | case STATUS_FLOAT_DIVIDE_BY_ZERO: | |
1221 | OUTMSG2 (("STATUS_FLOAT_DIVIDE_BY_ZERO")); | |
a493e3e2 | 1222 | ourstatus->value.sig = GDB_SIGNAL_FPE; |
b80864fb DJ |
1223 | break; |
1224 | case STATUS_INTEGER_DIVIDE_BY_ZERO: | |
1225 | OUTMSG2 (("STATUS_INTEGER_DIVIDE_BY_ZERO")); | |
a493e3e2 | 1226 | ourstatus->value.sig = GDB_SIGNAL_FPE; |
b80864fb DJ |
1227 | break; |
1228 | case STATUS_INTEGER_OVERFLOW: | |
1229 | OUTMSG2 (("STATUS_INTEGER_OVERFLOW")); | |
a493e3e2 | 1230 | ourstatus->value.sig = GDB_SIGNAL_FPE; |
b80864fb DJ |
1231 | break; |
1232 | case EXCEPTION_BREAKPOINT: | |
1233 | OUTMSG2 (("EXCEPTION_BREAKPOINT")); | |
a493e3e2 | 1234 | ourstatus->value.sig = GDB_SIGNAL_TRAP; |
ed50f18f PA |
1235 | #ifdef _WIN32_WCE |
1236 | /* Remove the initial breakpoint. */ | |
1237 | check_breakpoints ((CORE_ADDR) (long) current_event | |
1b3f6016 | 1238 | .u.Exception.ExceptionRecord.ExceptionAddress); |
ed50f18f | 1239 | #endif |
b80864fb DJ |
1240 | break; |
1241 | case DBG_CONTROL_C: | |
1242 | OUTMSG2 (("DBG_CONTROL_C")); | |
a493e3e2 | 1243 | ourstatus->value.sig = GDB_SIGNAL_INT; |
b80864fb DJ |
1244 | break; |
1245 | case DBG_CONTROL_BREAK: | |
1246 | OUTMSG2 (("DBG_CONTROL_BREAK")); | |
a493e3e2 | 1247 | ourstatus->value.sig = GDB_SIGNAL_INT; |
b80864fb DJ |
1248 | break; |
1249 | case EXCEPTION_SINGLE_STEP: | |
1250 | OUTMSG2 (("EXCEPTION_SINGLE_STEP")); | |
a493e3e2 | 1251 | ourstatus->value.sig = GDB_SIGNAL_TRAP; |
b80864fb DJ |
1252 | break; |
1253 | case EXCEPTION_ILLEGAL_INSTRUCTION: | |
1254 | OUTMSG2 (("EXCEPTION_ILLEGAL_INSTRUCTION")); | |
a493e3e2 | 1255 | ourstatus->value.sig = GDB_SIGNAL_ILL; |
b80864fb DJ |
1256 | break; |
1257 | case EXCEPTION_PRIV_INSTRUCTION: | |
1258 | OUTMSG2 (("EXCEPTION_PRIV_INSTRUCTION")); | |
a493e3e2 | 1259 | ourstatus->value.sig = GDB_SIGNAL_ILL; |
b80864fb DJ |
1260 | break; |
1261 | case EXCEPTION_NONCONTINUABLE_EXCEPTION: | |
1262 | OUTMSG2 (("EXCEPTION_NONCONTINUABLE_EXCEPTION")); | |
a493e3e2 | 1263 | ourstatus->value.sig = GDB_SIGNAL_ILL; |
b80864fb DJ |
1264 | break; |
1265 | default: | |
1266 | if (current_event.u.Exception.dwFirstChance) | |
34b34921 PA |
1267 | { |
1268 | ourstatus->kind = TARGET_WAITKIND_SPURIOUS; | |
1269 | return; | |
1270 | } | |
dfe07582 CV |
1271 | OUTMSG2 (("gdbserver: unknown target exception 0x%08x at 0x%s", |
1272 | (unsigned) current_event.u.Exception.ExceptionRecord.ExceptionCode, | |
1273 | phex_nz ((uintptr_t) current_event.u.Exception.ExceptionRecord. | |
1274 | ExceptionAddress, sizeof (uintptr_t)))); | |
a493e3e2 | 1275 | ourstatus->value.sig = GDB_SIGNAL_UNKNOWN; |
b80864fb DJ |
1276 | break; |
1277 | } | |
1278 | OUTMSG2 (("\n")); | |
1279 | last_sig = ourstatus->value.sig; | |
b80864fb DJ |
1280 | } |
1281 | ||
4d5d1aaa | 1282 | |
34b34921 | 1283 | static void |
9c80ecd6 | 1284 | suspend_one_thread (thread_info *thread) |
4d5d1aaa | 1285 | { |
e56f8ccb | 1286 | windows_thread_info *th = (windows_thread_info *) thread_target_data (thread); |
4d5d1aaa | 1287 | |
98a03287 | 1288 | th->suspend (); |
4d5d1aaa PA |
1289 | } |
1290 | ||
1291 | static void | |
1292 | fake_breakpoint_event (void) | |
b80864fb | 1293 | { |
4d5d1aaa | 1294 | OUTMSG2(("fake_breakpoint_event\n")); |
b80864fb | 1295 | |
4d5d1aaa PA |
1296 | faked_breakpoint = 1; |
1297 | ||
1298 | memset (¤t_event, 0, sizeof (current_event)); | |
1299 | current_event.dwThreadId = main_thread_id; | |
1300 | current_event.dwDebugEventCode = EXCEPTION_DEBUG_EVENT; | |
1301 | current_event.u.Exception.ExceptionRecord.ExceptionCode | |
1302 | = EXCEPTION_BREAKPOINT; | |
1303 | ||
f0045347 | 1304 | for_each_thread (suspend_one_thread); |
4d5d1aaa PA |
1305 | } |
1306 | ||
b65d95c5 DJ |
1307 | #ifdef _WIN32_WCE |
1308 | static int | |
1309 | auto_delete_breakpoint (CORE_ADDR stop_pc) | |
1310 | { | |
1311 | return 1; | |
1312 | } | |
1313 | #endif | |
1314 | ||
4d5d1aaa PA |
1315 | /* Get the next event from the child. */ |
1316 | ||
1317 | static int | |
1318 | get_child_debug_event (struct target_waitstatus *ourstatus) | |
1319 | { | |
95954743 PA |
1320 | ptid_t ptid; |
1321 | ||
a493e3e2 | 1322 | last_sig = GDB_SIGNAL_0; |
b80864fb DJ |
1323 | ourstatus->kind = TARGET_WAITKIND_SPURIOUS; |
1324 | ||
4d5d1aaa PA |
1325 | /* Check if GDB sent us an interrupt request. */ |
1326 | check_remote_input_interrupt_request (); | |
1327 | ||
1328 | if (soft_interrupt_requested) | |
1329 | { | |
1330 | soft_interrupt_requested = 0; | |
1331 | fake_breakpoint_event (); | |
1332 | goto gotevent; | |
1333 | } | |
1334 | ||
d97903b2 PA |
1335 | #ifndef _WIN32_WCE |
1336 | attaching = 0; | |
1337 | #else | |
1338 | if (attaching) | |
1339 | { | |
1340 | /* WinCE doesn't set an initial breakpoint automatically. To | |
1b3f6016 PA |
1341 | stop the inferior, we flush all currently pending debug |
1342 | events -- the thread list and the dll list are always | |
1343 | reported immediatelly without delay, then, we suspend all | |
1344 | threads and pretend we saw a trap at the current PC of the | |
1345 | main thread. | |
1346 | ||
1347 | Contrary to desktop Windows, Windows CE *does* report the dll | |
1348 | names on LOAD_DLL_DEBUG_EVENTs resulting from a | |
1349 | DebugActiveProcess call. This limits the way we can detect | |
1350 | if all the dlls have already been reported. If we get a real | |
1351 | debug event before leaving attaching, the worst that will | |
1352 | happen is the user will see a spurious breakpoint. */ | |
d97903b2 PA |
1353 | |
1354 | current_event.dwDebugEventCode = 0; | |
1355 | if (!WaitForDebugEvent (¤t_event, 0)) | |
1b3f6016 PA |
1356 | { |
1357 | OUTMSG2(("no attach events left\n")); | |
1358 | fake_breakpoint_event (); | |
1359 | attaching = 0; | |
1360 | } | |
d97903b2 | 1361 | else |
1b3f6016 | 1362 | OUTMSG2(("got attach event\n")); |
d97903b2 PA |
1363 | } |
1364 | else | |
1365 | #endif | |
1366 | { | |
30baf67b | 1367 | /* Keep the wait time low enough for comfortable remote |
1b3f6016 PA |
1368 | interruption, but high enough so gdbserver doesn't become a |
1369 | bottleneck. */ | |
d97903b2 | 1370 | if (!WaitForDebugEvent (¤t_event, 250)) |
912cf4ba PA |
1371 | { |
1372 | DWORD e = GetLastError(); | |
1373 | ||
1374 | if (e == ERROR_PIPE_NOT_CONNECTED) | |
1375 | { | |
1376 | /* This will happen if the loader fails to succesfully | |
1377 | load the application, e.g., if the main executable | |
1378 | tries to pull in a non-existing export from a | |
1379 | DLL. */ | |
1380 | ourstatus->kind = TARGET_WAITKIND_EXITED; | |
1381 | ourstatus->value.integer = 1; | |
1382 | return 1; | |
1383 | } | |
1384 | ||
1385 | return 0; | |
1386 | } | |
d97903b2 | 1387 | } |
4d5d1aaa PA |
1388 | |
1389 | gotevent: | |
b80864fb | 1390 | |
34b34921 | 1391 | switch (current_event.dwDebugEventCode) |
b80864fb DJ |
1392 | { |
1393 | case CREATE_THREAD_DEBUG_EVENT: | |
1394 | OUTMSG2 (("gdbserver: kernel event CREATE_THREAD_DEBUG_EVENT " | |
dfe07582 | 1395 | "for pid=%u tid=%x)\n", |
b80864fb DJ |
1396 | (unsigned) current_event.dwProcessId, |
1397 | (unsigned) current_event.dwThreadId)); | |
1398 | ||
1399 | /* Record the existence of this thread. */ | |
95954743 PA |
1400 | child_add_thread (current_event.dwProcessId, |
1401 | current_event.dwThreadId, | |
711e434b PM |
1402 | current_event.u.CreateThread.hThread, |
1403 | current_event.u.CreateThread.lpThreadLocalBase); | |
b80864fb DJ |
1404 | break; |
1405 | ||
1406 | case EXIT_THREAD_DEBUG_EVENT: | |
1407 | OUTMSG2 (("gdbserver: kernel event EXIT_THREAD_DEBUG_EVENT " | |
dfe07582 | 1408 | "for pid=%u tid=%x\n", |
b80864fb DJ |
1409 | (unsigned) current_event.dwProcessId, |
1410 | (unsigned) current_event.dwThreadId)); | |
95954743 PA |
1411 | child_delete_thread (current_event.dwProcessId, |
1412 | current_event.dwThreadId); | |
aeeb81d1 | 1413 | |
9c80ecd6 | 1414 | current_thread = get_first_thread (); |
aeeb81d1 | 1415 | return 1; |
b80864fb DJ |
1416 | |
1417 | case CREATE_PROCESS_DEBUG_EVENT: | |
1418 | OUTMSG2 (("gdbserver: kernel event CREATE_PROCESS_DEBUG_EVENT " | |
dfe07582 | 1419 | "for pid=%u tid=%x\n", |
b80864fb DJ |
1420 | (unsigned) current_event.dwProcessId, |
1421 | (unsigned) current_event.dwThreadId)); | |
1422 | CloseHandle (current_event.u.CreateProcessInfo.hFile); | |
1423 | ||
1424 | current_process_handle = current_event.u.CreateProcessInfo.hProcess; | |
1425 | main_thread_id = current_event.dwThreadId; | |
1426 | ||
b80864fb | 1427 | /* Add the main thread. */ |
95954743 PA |
1428 | child_add_thread (current_event.dwProcessId, |
1429 | main_thread_id, | |
711e434b PM |
1430 | current_event.u.CreateProcessInfo.hThread, |
1431 | current_event.u.CreateProcessInfo.lpThreadLocalBase); | |
b80864fb | 1432 | |
ed50f18f | 1433 | #ifdef _WIN32_WCE |
d97903b2 PA |
1434 | if (!attaching) |
1435 | { | |
1436 | /* Windows CE doesn't set the initial breakpoint | |
1437 | automatically like the desktop versions of Windows do. | |
1438 | We add it explicitly here. It will be removed as soon as | |
1439 | it is hit. */ | |
1440 | set_breakpoint_at ((CORE_ADDR) (long) current_event.u | |
1441 | .CreateProcessInfo.lpStartAddress, | |
b65d95c5 | 1442 | auto_delete_breakpoint); |
d97903b2 | 1443 | } |
ed50f18f | 1444 | #endif |
b80864fb DJ |
1445 | break; |
1446 | ||
1447 | case EXIT_PROCESS_DEBUG_EVENT: | |
1448 | OUTMSG2 (("gdbserver: kernel event EXIT_PROCESS_DEBUG_EVENT " | |
dfe07582 | 1449 | "for pid=%u tid=%x\n", |
b80864fb DJ |
1450 | (unsigned) current_event.dwProcessId, |
1451 | (unsigned) current_event.dwThreadId)); | |
559e7e50 EZ |
1452 | { |
1453 | DWORD exit_status = current_event.u.ExitProcess.dwExitCode; | |
1454 | /* If the exit status looks like a fatal exception, but we | |
1455 | don't recognize the exception's code, make the original | |
1456 | exit status value available, to avoid losing information. */ | |
1457 | int exit_signal | |
1458 | = WIFSIGNALED (exit_status) ? WTERMSIG (exit_status) : -1; | |
1459 | if (exit_signal == -1) | |
1460 | { | |
1461 | ourstatus->kind = TARGET_WAITKIND_EXITED; | |
1462 | ourstatus->value.integer = exit_status; | |
1463 | } | |
1464 | else | |
1465 | { | |
1466 | ourstatus->kind = TARGET_WAITKIND_SIGNALLED; | |
1467 | ourstatus->value.sig = gdb_signal_from_host (exit_signal); | |
1468 | } | |
1469 | } | |
18aae699 | 1470 | child_continue (DBG_CONTINUE, -1); |
b80864fb | 1471 | CloseHandle (current_process_handle); |
9d606399 | 1472 | current_process_handle = NULL; |
b80864fb DJ |
1473 | break; |
1474 | ||
1475 | case LOAD_DLL_DEBUG_EVENT: | |
1476 | OUTMSG2 (("gdbserver: kernel event LOAD_DLL_DEBUG_EVENT " | |
dfe07582 | 1477 | "for pid=%u tid=%x\n", |
b80864fb DJ |
1478 | (unsigned) current_event.dwProcessId, |
1479 | (unsigned) current_event.dwThreadId)); | |
1480 | CloseHandle (current_event.u.LoadDll.hFile); | |
f25b3fc3 JB |
1481 | if (! child_initialization_done) |
1482 | break; | |
255e7678 | 1483 | handle_load_dll (); |
b80864fb DJ |
1484 | |
1485 | ourstatus->kind = TARGET_WAITKIND_LOADED; | |
a493e3e2 | 1486 | ourstatus->value.sig = GDB_SIGNAL_TRAP; |
b80864fb DJ |
1487 | break; |
1488 | ||
1489 | case UNLOAD_DLL_DEBUG_EVENT: | |
1490 | OUTMSG2 (("gdbserver: kernel event UNLOAD_DLL_DEBUG_EVENT " | |
dfe07582 | 1491 | "for pid=%u tid=%x\n", |
b80864fb DJ |
1492 | (unsigned) current_event.dwProcessId, |
1493 | (unsigned) current_event.dwThreadId)); | |
f25b3fc3 JB |
1494 | if (! child_initialization_done) |
1495 | break; | |
255e7678 DJ |
1496 | handle_unload_dll (); |
1497 | ourstatus->kind = TARGET_WAITKIND_LOADED; | |
a493e3e2 | 1498 | ourstatus->value.sig = GDB_SIGNAL_TRAP; |
b80864fb DJ |
1499 | break; |
1500 | ||
1501 | case EXCEPTION_DEBUG_EVENT: | |
1502 | OUTMSG2 (("gdbserver: kernel event EXCEPTION_DEBUG_EVENT " | |
dfe07582 | 1503 | "for pid=%u tid=%x\n", |
b80864fb DJ |
1504 | (unsigned) current_event.dwProcessId, |
1505 | (unsigned) current_event.dwThreadId)); | |
34b34921 | 1506 | handle_exception (ourstatus); |
b80864fb DJ |
1507 | break; |
1508 | ||
1509 | case OUTPUT_DEBUG_STRING_EVENT: | |
1510 | /* A message from the kernel (or Cygwin). */ | |
1511 | OUTMSG2 (("gdbserver: kernel event OUTPUT_DEBUG_STRING_EVENT " | |
dfe07582 | 1512 | "for pid=%u tid=%x\n", |
b80864fb DJ |
1513 | (unsigned) current_event.dwProcessId, |
1514 | (unsigned) current_event.dwThreadId)); | |
c0879059 | 1515 | handle_output_debug_string (); |
b80864fb DJ |
1516 | break; |
1517 | ||
1518 | default: | |
1519 | OUTMSG2 (("gdbserver: kernel event unknown " | |
dfe07582 | 1520 | "for pid=%u tid=%x code=%x\n", |
b80864fb DJ |
1521 | (unsigned) current_event.dwProcessId, |
1522 | (unsigned) current_event.dwThreadId, | |
dfe07582 | 1523 | (unsigned) current_event.dwDebugEventCode)); |
b80864fb DJ |
1524 | break; |
1525 | } | |
1526 | ||
aeeb81d1 | 1527 | ptid = debug_event_ptid (¤t_event); |
8dc7b443 | 1528 | current_thread = find_thread_ptid (ptid); |
4d5d1aaa | 1529 | return 1; |
b80864fb DJ |
1530 | } |
1531 | ||
1532 | /* Wait for the inferior process to change state. | |
1533 | STATUS will be filled in with a response code to send to GDB. | |
1534 | Returns the signal which caused the process to stop. */ | |
6532e7e3 TBA |
1535 | ptid_t |
1536 | win32_process_target::wait (ptid_t ptid, target_waitstatus *ourstatus, | |
1537 | int options) | |
b80864fb | 1538 | { |
442ea881 | 1539 | struct regcache *regcache; |
95954743 | 1540 | |
4210d83e PA |
1541 | if (cached_status.kind != TARGET_WAITKIND_IGNORE) |
1542 | { | |
1543 | /* The core always does a wait after creating the inferior, and | |
1544 | do_initial_child_stuff already ran the inferior to the | |
1545 | initial breakpoint (or an exit, if creating the process | |
1546 | fails). Report it now. */ | |
1547 | *ourstatus = cached_status; | |
1548 | cached_status.kind = TARGET_WAITKIND_IGNORE; | |
1549 | return debug_event_ptid (¤t_event); | |
1550 | } | |
1551 | ||
b80864fb DJ |
1552 | while (1) |
1553 | { | |
5b1c542e | 1554 | if (!get_child_debug_event (ourstatus)) |
4d5d1aaa | 1555 | continue; |
b80864fb | 1556 | |
5b1c542e | 1557 | switch (ourstatus->kind) |
b80864fb | 1558 | { |
34b34921 | 1559 | case TARGET_WAITKIND_EXITED: |
b80864fb | 1560 | OUTMSG2 (("Child exited with retcode = %x\n", |
5b1c542e | 1561 | ourstatus->value.integer)); |
5ac588cf | 1562 | win32_clear_inferiors (); |
f2907e49 | 1563 | return ptid_t (current_event.dwProcessId); |
34b34921 | 1564 | case TARGET_WAITKIND_STOPPED: |
559e7e50 | 1565 | case TARGET_WAITKIND_SIGNALLED: |
1b3f6016 | 1566 | case TARGET_WAITKIND_LOADED: |
f72f3e60 | 1567 | OUTMSG2 (("Child Stopped with signal = %d \n", |
10357975 | 1568 | ourstatus->value.sig)); |
b80864fb | 1569 | |
0bfdf32f | 1570 | regcache = get_thread_regcache (current_thread, 1); |
442ea881 | 1571 | child_fetch_inferior_registers (regcache, -1); |
95954743 | 1572 | return debug_event_ptid (¤t_event); |
1b3f6016 | 1573 | default: |
5b1c542e | 1574 | OUTMSG (("Ignoring unknown internal event, %d\n", ourstatus->kind)); |
1b3f6016 PA |
1575 | /* fall-through */ |
1576 | case TARGET_WAITKIND_SPURIOUS: | |
34b34921 PA |
1577 | /* do nothing, just continue */ |
1578 | child_continue (DBG_CONTINUE, -1); | |
1579 | break; | |
b80864fb | 1580 | } |
b80864fb DJ |
1581 | } |
1582 | } | |
1583 | ||
1584 | /* Fetch registers from the inferior process. | |
1585 | If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */ | |
a5a4d4cd TBA |
1586 | void |
1587 | win32_process_target::fetch_registers (regcache *regcache, int regno) | |
b80864fb | 1588 | { |
442ea881 | 1589 | child_fetch_inferior_registers (regcache, regno); |
b80864fb DJ |
1590 | } |
1591 | ||
1592 | /* Store registers to the inferior process. | |
1593 | If REGNO is -1, store all registers; otherwise, store at least REGNO. */ | |
a5a4d4cd TBA |
1594 | void |
1595 | win32_process_target::store_registers (regcache *regcache, int regno) | |
b80864fb | 1596 | { |
442ea881 | 1597 | child_store_inferior_registers (regcache, regno); |
b80864fb DJ |
1598 | } |
1599 | ||
1600 | /* Read memory from the inferior process. This should generally be | |
1601 | called through read_inferior_memory, which handles breakpoint shadowing. | |
1602 | Read LEN bytes at MEMADDR into a buffer at MYADDR. */ | |
e2558df3 TBA |
1603 | int |
1604 | win32_process_target::read_memory (CORE_ADDR memaddr, unsigned char *myaddr, | |
1605 | int len) | |
b80864fb | 1606 | { |
ed50f18f | 1607 | return child_xfer_memory (memaddr, (char *) myaddr, len, 0, 0) != len; |
b80864fb DJ |
1608 | } |
1609 | ||
1610 | /* Write memory to the inferior process. This should generally be | |
1611 | called through write_inferior_memory, which handles breakpoint shadowing. | |
1612 | Write LEN bytes from the buffer at MYADDR to MEMADDR. | |
1613 | Returns 0 on success and errno on failure. */ | |
e2558df3 TBA |
1614 | int |
1615 | win32_process_target::write_memory (CORE_ADDR memaddr, | |
1616 | const unsigned char *myaddr, int len) | |
b80864fb DJ |
1617 | { |
1618 | return child_xfer_memory (memaddr, (char *) myaddr, len, 1, 0) != len; | |
1619 | } | |
1620 | ||
7390519e | 1621 | /* Send an interrupt request to the inferior process. */ |
eb497a2a TBA |
1622 | void |
1623 | win32_process_target::request_interrupt () | |
7390519e PA |
1624 | { |
1625 | winapi_DebugBreakProcess DebugBreakProcess; | |
1626 | winapi_GenerateConsoleCtrlEvent GenerateConsoleCtrlEvent; | |
1627 | ||
1628 | #ifdef _WIN32_WCE | |
1629 | HMODULE dll = GetModuleHandle (_T("COREDLL.DLL")); | |
1630 | #else | |
1631 | HMODULE dll = GetModuleHandle (_T("KERNEL32.DLL")); | |
1632 | #endif | |
1633 | ||
1634 | GenerateConsoleCtrlEvent = GETPROCADDRESS (dll, GenerateConsoleCtrlEvent); | |
1635 | ||
1636 | if (GenerateConsoleCtrlEvent != NULL | |
1637 | && GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT, current_process_id)) | |
1638 | return; | |
1639 | ||
1640 | /* GenerateConsoleCtrlEvent can fail if process id being debugged is | |
1641 | not a process group id. | |
1642 | Fallback to XP/Vista 'DebugBreakProcess', which generates a | |
1643 | breakpoint exception in the interior process. */ | |
1644 | ||
1645 | DebugBreakProcess = GETPROCADDRESS (dll, DebugBreakProcess); | |
1646 | ||
1647 | if (DebugBreakProcess != NULL | |
1648 | && DebugBreakProcess (current_process_handle)) | |
1649 | return; | |
1650 | ||
4d5d1aaa PA |
1651 | /* Last resort, suspend all threads manually. */ |
1652 | soft_interrupt_requested = 1; | |
7390519e PA |
1653 | } |
1654 | ||
22aa6223 TBA |
1655 | bool |
1656 | win32_process_target::supports_hardware_single_step () | |
1657 | { | |
1658 | return true; | |
1659 | } | |
1660 | ||
59a016f0 PA |
1661 | #ifdef _WIN32_WCE |
1662 | int | |
1663 | win32_error_to_fileio_error (DWORD err) | |
1664 | { | |
1665 | switch (err) | |
1666 | { | |
1667 | case ERROR_BAD_PATHNAME: | |
1668 | case ERROR_FILE_NOT_FOUND: | |
1669 | case ERROR_INVALID_NAME: | |
1670 | case ERROR_PATH_NOT_FOUND: | |
1671 | return FILEIO_ENOENT; | |
1672 | case ERROR_CRC: | |
1673 | case ERROR_IO_DEVICE: | |
1674 | case ERROR_OPEN_FAILED: | |
1675 | return FILEIO_EIO; | |
1676 | case ERROR_INVALID_HANDLE: | |
1677 | return FILEIO_EBADF; | |
1678 | case ERROR_ACCESS_DENIED: | |
1679 | case ERROR_SHARING_VIOLATION: | |
1680 | return FILEIO_EACCES; | |
1681 | case ERROR_NOACCESS: | |
1682 | return FILEIO_EFAULT; | |
1683 | case ERROR_BUSY: | |
1684 | return FILEIO_EBUSY; | |
1685 | case ERROR_ALREADY_EXISTS: | |
1686 | case ERROR_FILE_EXISTS: | |
1687 | return FILEIO_EEXIST; | |
1688 | case ERROR_BAD_DEVICE: | |
1689 | return FILEIO_ENODEV; | |
1690 | case ERROR_DIRECTORY: | |
1691 | return FILEIO_ENOTDIR; | |
1692 | case ERROR_FILENAME_EXCED_RANGE: | |
1693 | case ERROR_INVALID_DATA: | |
1694 | case ERROR_INVALID_PARAMETER: | |
1695 | case ERROR_NEGATIVE_SEEK: | |
1696 | return FILEIO_EINVAL; | |
1697 | case ERROR_TOO_MANY_OPEN_FILES: | |
1698 | return FILEIO_EMFILE; | |
1699 | case ERROR_HANDLE_DISK_FULL: | |
1700 | case ERROR_DISK_FULL: | |
1701 | return FILEIO_ENOSPC; | |
1702 | case ERROR_WRITE_PROTECT: | |
1703 | return FILEIO_EROFS; | |
1704 | case ERROR_NOT_SUPPORTED: | |
1705 | return FILEIO_ENOSYS; | |
1706 | } | |
1707 | ||
1708 | return FILEIO_EUNKNOWN; | |
1709 | } | |
1710 | ||
ea06bbaa TBA |
1711 | void |
1712 | win32_process_target::hostio_last_error (char *buf) | |
59a016f0 PA |
1713 | { |
1714 | DWORD winerr = GetLastError (); | |
1715 | int fileio_err = win32_error_to_fileio_error (winerr); | |
1716 | sprintf (buf, "F-1,%x", fileio_err); | |
1717 | } | |
1718 | #endif | |
1719 | ||
d7abedf7 TBA |
1720 | bool |
1721 | win32_process_target::supports_qxfer_siginfo () | |
1722 | { | |
1723 | return true; | |
1724 | } | |
1725 | ||
7928d571 HD |
1726 | /* Write Windows signal info. */ |
1727 | ||
d7abedf7 TBA |
1728 | int |
1729 | win32_process_target::qxfer_siginfo (const char *annex, | |
1730 | unsigned char *readbuf, | |
1731 | unsigned const char *writebuf, | |
1732 | CORE_ADDR offset, int len) | |
7928d571 HD |
1733 | { |
1734 | if (siginfo_er.ExceptionCode == 0) | |
1735 | return -1; | |
1736 | ||
1737 | if (readbuf == nullptr) | |
1738 | return -1; | |
1739 | ||
1740 | if (offset > sizeof (siginfo_er)) | |
1741 | return -1; | |
1742 | ||
1743 | if (offset + len > sizeof (siginfo_er)) | |
1744 | len = sizeof (siginfo_er) - offset; | |
1745 | ||
1746 | memcpy (readbuf, (char *) &siginfo_er + offset, len); | |
1747 | ||
1748 | return len; | |
1749 | } | |
1750 | ||
4e2e869c TBA |
1751 | bool |
1752 | win32_process_target::supports_get_tib_address () | |
1753 | { | |
1754 | return true; | |
1755 | } | |
1756 | ||
711e434b PM |
1757 | /* Write Windows OS Thread Information Block address. */ |
1758 | ||
4e2e869c TBA |
1759 | int |
1760 | win32_process_target::get_tib_address (ptid_t ptid, CORE_ADDR *addr) | |
711e434b | 1761 | { |
e56f8ccb | 1762 | windows_thread_info *th; |
28688adf | 1763 | th = thread_rec (ptid, DONT_INVALIDATE_CONTEXT); |
711e434b PM |
1764 | if (th == NULL) |
1765 | return 0; | |
1766 | if (addr != NULL) | |
1767 | *addr = th->thread_local_base; | |
1768 | return 1; | |
1769 | } | |
1770 | ||
fb78e89c AT |
1771 | /* Implementation of the target_ops method "sw_breakpoint_from_kind". */ |
1772 | ||
d367006f TBA |
1773 | const gdb_byte * |
1774 | win32_process_target::sw_breakpoint_from_kind (int kind, int *size) | |
fb78e89c AT |
1775 | { |
1776 | *size = the_low_target.breakpoint_len; | |
1777 | return the_low_target.breakpoint; | |
1778 | } | |
1779 | ||
5ef9273d TBA |
1780 | /* The win32 target ops object. */ |
1781 | ||
1782 | static win32_process_target the_win32_target; | |
1783 | ||
b80864fb DJ |
1784 | /* Initialize the Win32 backend. */ |
1785 | void | |
1786 | initialize_low (void) | |
1787 | { | |
52405d85 | 1788 | set_target_ops (&the_win32_target); |
d05b4ac3 | 1789 | the_low_target.arch_setup (); |
b80864fb | 1790 | } |