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