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