Commit | Line | Data |
---|---|---|
b80864fb | 1 | /* Low level interface to Windows debugging, for gdbserver. |
6aba47ca | 2 | Copyright (C) 2006, 2007 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 | |
10 | the Free Software Foundation; either version 2 of the License, or | |
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 | |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
21 | Boston, MA 02110-1301, USA. */ | |
22 | ||
23 | #include "server.h" | |
24 | #include "regcache.h" | |
25 | #include "gdb/signals.h" | |
ed50f18f PA |
26 | #include "mem-break.h" |
27 | #include "win32-low.h" | |
b80864fb DJ |
28 | |
29 | #include <windows.h> | |
ed50f18f | 30 | #include <winnt.h> |
b80864fb | 31 | #include <imagehlp.h> |
255e7678 | 32 | #include <tlhelp32.h> |
b80864fb DJ |
33 | #include <psapi.h> |
34 | #include <sys/param.h> | |
35 | #include <malloc.h> | |
36 | #include <process.h> | |
37 | ||
38 | #ifndef USE_WIN32API | |
39 | #include <sys/cygwin.h> | |
40 | #endif | |
41 | ||
42 | #define LOG 0 | |
43 | ||
44 | #define OUTMSG(X) do { printf X; fflush (stdout); } while (0) | |
45 | #if LOG | |
46 | #define OUTMSG2(X) do { printf X; fflush (stdout); } while (0) | |
47 | #else | |
ed50f18f PA |
48 | #define OUTMSG2(X) do ; while (0) |
49 | #endif | |
50 | ||
51 | #ifndef _T | |
52 | #define _T(x) TEXT (x) | |
53 | #endif | |
54 | ||
55 | #ifndef COUNTOF | |
56 | #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0])) | |
b80864fb DJ |
57 | #endif |
58 | ||
bf914831 PA |
59 | #ifdef _WIN32_WCE |
60 | # define GETPROCADDRESS(DLL, PROC) \ | |
61 | ((winapi_ ## PROC) GetProcAddress (DLL, TEXT (#PROC))) | |
62 | #else | |
63 | # define GETPROCADDRESS(DLL, PROC) \ | |
64 | ((winapi_ ## PROC) GetProcAddress (DLL, #PROC)) | |
65 | #endif | |
66 | ||
b80864fb DJ |
67 | int using_threads = 1; |
68 | ||
69 | /* Globals. */ | |
70 | static HANDLE current_process_handle = NULL; | |
71 | static DWORD current_process_id = 0; | |
72 | static enum target_signal last_sig = TARGET_SIGNAL_0; | |
73 | ||
74 | /* The current debug event from WaitForDebugEvent. */ | |
75 | static DEBUG_EVENT current_event; | |
76 | ||
ed50f18f | 77 | #define NUM_REGS (the_low_target.num_regs) |
b80864fb | 78 | |
bf914831 PA |
79 | typedef BOOL WINAPI (*winapi_DebugActiveProcessStop) (DWORD dwProcessId); |
80 | typedef BOOL WINAPI (*winapi_DebugSetProcessKillOnExit) (BOOL KillOnExit); | |
7390519e PA |
81 | typedef BOOL WINAPI (*winapi_DebugBreakProcess) (HANDLE); |
82 | typedef BOOL WINAPI (*winapi_GenerateConsoleCtrlEvent) (DWORD, DWORD); | |
b80864fb | 83 | |
b80864fb DJ |
84 | static DWORD main_thread_id = 0; |
85 | ||
34b34921 PA |
86 | static void win32_resume (struct thread_resume *resume_info); |
87 | ||
b80864fb DJ |
88 | /* Get the thread ID from the current selected inferior (the current |
89 | thread). */ | |
90 | static DWORD | |
91 | current_inferior_tid (void) | |
92 | { | |
41093d81 | 93 | win32_thread_info *th = inferior_target_data (current_inferior); |
b80864fb DJ |
94 | return th->tid; |
95 | } | |
96 | ||
97 | /* Find a thread record given a thread id. If GET_CONTEXT is set then | |
98 | also retrieve the context for this thread. */ | |
41093d81 | 99 | static win32_thread_info * |
b80864fb DJ |
100 | thread_rec (DWORD id, int get_context) |
101 | { | |
102 | struct thread_info *thread; | |
41093d81 | 103 | win32_thread_info *th; |
b80864fb DJ |
104 | |
105 | thread = (struct thread_info *) find_inferior_id (&all_threads, id); | |
106 | if (thread == NULL) | |
107 | return NULL; | |
108 | ||
109 | th = inferior_target_data (thread); | |
110 | if (!th->suspend_count && get_context) | |
111 | { | |
34b34921 | 112 | if (id != current_event.dwThreadId) |
b80864fb | 113 | th->suspend_count = SuspendThread (th->h) + 1; |
b80864fb | 114 | |
34b34921 | 115 | (*the_low_target.get_thread_context) (th, ¤t_event); |
b80864fb DJ |
116 | } |
117 | ||
118 | return th; | |
119 | } | |
120 | ||
121 | /* Add a thread to the thread list. */ | |
41093d81 | 122 | static win32_thread_info * |
b80864fb DJ |
123 | child_add_thread (DWORD tid, HANDLE h) |
124 | { | |
41093d81 | 125 | win32_thread_info *th; |
b80864fb DJ |
126 | |
127 | if ((th = thread_rec (tid, FALSE))) | |
128 | return th; | |
129 | ||
41093d81 | 130 | th = (win32_thread_info *) malloc (sizeof (*th)); |
b80864fb DJ |
131 | memset (th, 0, sizeof (*th)); |
132 | th->tid = tid; | |
133 | th->h = h; | |
134 | ||
135 | add_thread (tid, th, (unsigned int) tid); | |
136 | set_inferior_regcache_data ((struct thread_info *) | |
137 | find_inferior_id (&all_threads, tid), | |
138 | new_register_cache ()); | |
139 | ||
34b34921 PA |
140 | if (the_low_target.thread_added != NULL) |
141 | (*the_low_target.thread_added) (th); | |
b80864fb DJ |
142 | |
143 | return th; | |
144 | } | |
145 | ||
146 | /* Delete a thread from the list of threads. */ | |
147 | static void | |
148 | delete_thread_info (struct inferior_list_entry *thread) | |
149 | { | |
41093d81 | 150 | win32_thread_info *th = inferior_target_data ((struct thread_info *) thread); |
b80864fb DJ |
151 | |
152 | remove_thread ((struct thread_info *) thread); | |
153 | CloseHandle (th->h); | |
154 | free (th); | |
155 | } | |
156 | ||
157 | /* Delete a thread from the list of threads. */ | |
158 | static void | |
159 | child_delete_thread (DWORD id) | |
160 | { | |
161 | struct inferior_list_entry *thread; | |
162 | ||
163 | /* If the last thread is exiting, just return. */ | |
164 | if (all_threads.head == all_threads.tail) | |
165 | return; | |
166 | ||
167 | thread = find_inferior_id (&all_threads, id); | |
168 | if (thread == NULL) | |
169 | return; | |
170 | ||
171 | delete_thread_info (thread); | |
172 | } | |
173 | ||
174 | /* Transfer memory from/to the debugged process. */ | |
175 | static int | |
176 | child_xfer_memory (CORE_ADDR memaddr, char *our, int len, | |
177 | int write, struct target_ops *target) | |
178 | { | |
179 | SIZE_T done; | |
180 | long addr = (long) memaddr; | |
181 | ||
182 | if (write) | |
183 | { | |
184 | WriteProcessMemory (current_process_handle, (LPVOID) addr, | |
185 | (LPCVOID) our, len, &done); | |
186 | FlushInstructionCache (current_process_handle, (LPCVOID) addr, len); | |
187 | } | |
188 | else | |
189 | { | |
190 | ReadProcessMemory (current_process_handle, (LPCVOID) addr, (LPVOID) our, | |
191 | len, &done); | |
192 | } | |
193 | return done; | |
194 | } | |
195 | ||
196 | /* Generally, what has the program done? */ | |
197 | enum target_waitkind | |
198 | { | |
199 | /* The program has exited. The exit status is in value.integer. */ | |
200 | TARGET_WAITKIND_EXITED, | |
201 | ||
202 | /* The program has stopped with a signal. Which signal is in | |
203 | value.sig. */ | |
204 | TARGET_WAITKIND_STOPPED, | |
205 | ||
255e7678 DJ |
206 | /* The program is letting us know that it dynamically loaded |
207 | or unloaded something. */ | |
b80864fb DJ |
208 | TARGET_WAITKIND_LOADED, |
209 | ||
210 | /* The program has exec'ed a new executable file. The new file's | |
211 | pathname is pointed to by value.execd_pathname. */ | |
b80864fb DJ |
212 | TARGET_WAITKIND_EXECD, |
213 | ||
7390519e PA |
214 | /* Nothing interesting happened, but we stopped anyway. We take the |
215 | chance to check if GDB requested an interrupt. */ | |
b80864fb DJ |
216 | TARGET_WAITKIND_SPURIOUS, |
217 | }; | |
218 | ||
219 | struct target_waitstatus | |
220 | { | |
221 | enum target_waitkind kind; | |
222 | ||
223 | /* Forked child pid, execd pathname, exit status or signal number. */ | |
224 | union | |
225 | { | |
226 | int integer; | |
227 | enum target_signal sig; | |
228 | int related_pid; | |
229 | char *execd_pathname; | |
230 | int syscall_id; | |
231 | } | |
232 | value; | |
233 | }; | |
234 | ||
ed50f18f | 235 | /* Clear out any old thread list and reinitialize it to a pristine |
b80864fb DJ |
236 | state. */ |
237 | static void | |
238 | child_init_thread_list (void) | |
239 | { | |
240 | for_each_inferior (&all_threads, delete_thread_info); | |
241 | } | |
242 | ||
243 | static void | |
244 | do_initial_child_stuff (DWORD pid) | |
245 | { | |
b80864fb DJ |
246 | last_sig = TARGET_SIGNAL_0; |
247 | ||
b80864fb DJ |
248 | memset (¤t_event, 0, sizeof (current_event)); |
249 | ||
250 | child_init_thread_list (); | |
ed50f18f PA |
251 | |
252 | if (the_low_target.initial_stuff != NULL) | |
253 | (*the_low_target.initial_stuff) (); | |
b80864fb DJ |
254 | } |
255 | ||
256 | /* Resume all artificially suspended threads if we are continuing | |
257 | execution. */ | |
258 | static int | |
259 | continue_one_thread (struct inferior_list_entry *this_thread, void *id_ptr) | |
260 | { | |
261 | struct thread_info *thread = (struct thread_info *) this_thread; | |
262 | int thread_id = * (int *) id_ptr; | |
41093d81 | 263 | win32_thread_info *th = inferior_target_data (thread); |
b80864fb DJ |
264 | int i; |
265 | ||
266 | if ((thread_id == -1 || thread_id == th->tid) | |
267 | && th->suspend_count) | |
268 | { | |
34b34921 | 269 | if (th->context.ContextFlags) |
b80864fb | 270 | { |
34b34921 | 271 | (*the_low_target.set_thread_context) (th, ¤t_event); |
b80864fb DJ |
272 | th->context.ContextFlags = 0; |
273 | } | |
34b34921 PA |
274 | |
275 | for (i = 0; i < th->suspend_count; i++) | |
276 | (void) ResumeThread (th->h); | |
277 | th->suspend_count = 0; | |
b80864fb DJ |
278 | } |
279 | ||
280 | return 0; | |
281 | } | |
282 | ||
283 | static BOOL | |
284 | child_continue (DWORD continue_status, int thread_id) | |
285 | { | |
286 | BOOL res; | |
287 | ||
288 | res = ContinueDebugEvent (current_event.dwProcessId, | |
289 | current_event.dwThreadId, continue_status); | |
b80864fb DJ |
290 | if (res) |
291 | find_inferior (&all_threads, continue_one_thread, &thread_id); | |
292 | ||
b80864fb DJ |
293 | return res; |
294 | } | |
295 | ||
b80864fb DJ |
296 | /* Fetch register(s) from the current thread context. */ |
297 | static void | |
298 | child_fetch_inferior_registers (int r) | |
299 | { | |
300 | int regno; | |
41093d81 | 301 | win32_thread_info *th = thread_rec (current_inferior_tid (), TRUE); |
b80864fb DJ |
302 | if (r == -1 || r == 0 || r > NUM_REGS) |
303 | child_fetch_inferior_registers (NUM_REGS); | |
304 | else | |
305 | for (regno = 0; regno < r; regno++) | |
34b34921 | 306 | (*the_low_target.fetch_inferior_register) (th, regno); |
b80864fb DJ |
307 | } |
308 | ||
309 | /* Store a new register value into the current thread context. We don't | |
310 | change the program's context until later, when we resume it. */ | |
311 | static void | |
312 | child_store_inferior_registers (int r) | |
313 | { | |
314 | int regno; | |
41093d81 | 315 | win32_thread_info *th = thread_rec (current_inferior_tid (), TRUE); |
b80864fb DJ |
316 | if (r == -1 || r == 0 || r > NUM_REGS) |
317 | child_store_inferior_registers (NUM_REGS); | |
318 | else | |
319 | for (regno = 0; regno < r; regno++) | |
34b34921 | 320 | (*the_low_target.store_inferior_register) (th, regno); |
b80864fb DJ |
321 | } |
322 | ||
ed50f18f PA |
323 | /* Map the Windows error number in ERROR to a locale-dependent error |
324 | message string and return a pointer to it. Typically, the values | |
325 | for ERROR come from GetLastError. | |
326 | ||
327 | The string pointed to shall not be modified by the application, | |
328 | but may be overwritten by a subsequent call to strwinerror | |
329 | ||
330 | The strwinerror function does not change the current setting | |
331 | of GetLastError. */ | |
332 | ||
333 | char * | |
334 | strwinerror (DWORD error) | |
335 | { | |
336 | static char buf[1024]; | |
337 | TCHAR *msgbuf; | |
338 | DWORD lasterr = GetLastError (); | |
339 | DWORD chars = FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | |
340 | | FORMAT_MESSAGE_ALLOCATE_BUFFER, | |
341 | NULL, | |
342 | error, | |
343 | 0, /* Default language */ | |
344 | (LPVOID)&msgbuf, | |
345 | 0, | |
346 | NULL); | |
347 | if (chars != 0) | |
348 | { | |
349 | /* If there is an \r\n appended, zap it. */ | |
350 | if (chars >= 2 | |
351 | && msgbuf[chars - 2] == '\r' | |
352 | && msgbuf[chars - 1] == '\n') | |
353 | { | |
354 | chars -= 2; | |
355 | msgbuf[chars] = 0; | |
356 | } | |
357 | ||
358 | if (chars > ((COUNTOF (buf)) - 1)) | |
359 | { | |
360 | chars = COUNTOF (buf) - 1; | |
361 | msgbuf [chars] = 0; | |
362 | } | |
363 | ||
364 | #ifdef UNICODE | |
365 | wcstombs (buf, msgbuf, chars + 1); | |
366 | #else | |
367 | strncpy (buf, msgbuf, chars + 1); | |
368 | #endif | |
369 | LocalFree (msgbuf); | |
370 | } | |
371 | else | |
372 | sprintf (buf, "unknown win32 error (%ld)", error); | |
373 | ||
374 | SetLastError (lasterr); | |
375 | return buf; | |
376 | } | |
377 | ||
b80864fb DJ |
378 | /* Start a new process. |
379 | PROGRAM is a path to the program to execute. | |
380 | ARGS is a standard NULL-terminated array of arguments, | |
381 | to be passed to the inferior as ``argv''. | |
382 | Returns the new PID on success, -1 on failure. Registers the new | |
383 | process with the process list. */ | |
384 | static int | |
385 | win32_create_inferior (char *program, char **program_args) | |
386 | { | |
387 | #ifndef USE_WIN32API | |
388 | char real_path[MAXPATHLEN]; | |
389 | char *orig_path, *new_path, *path_ptr; | |
390 | #endif | |
b80864fb DJ |
391 | BOOL ret; |
392 | DWORD flags; | |
393 | char *args; | |
394 | int argslen; | |
395 | int argc; | |
ed50f18f PA |
396 | PROCESS_INFORMATION pi; |
397 | #ifndef __MINGW32CE__ | |
bf914831 | 398 | STARTUPINFOA si = { sizeof (STARTUPINFOA) }; |
ed50f18f PA |
399 | char *winenv = NULL; |
400 | #else | |
401 | wchar_t *wargs, *wprogram; | |
402 | #endif | |
b80864fb DJ |
403 | |
404 | if (!program) | |
405 | error ("No executable specified, specify executable to debug.\n"); | |
406 | ||
b80864fb DJ |
407 | flags = DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS; |
408 | ||
409 | #ifndef USE_WIN32API | |
410 | orig_path = NULL; | |
411 | path_ptr = getenv ("PATH"); | |
412 | if (path_ptr) | |
413 | { | |
414 | orig_path = alloca (strlen (path_ptr) + 1); | |
415 | new_path = alloca (cygwin_posix_to_win32_path_list_buf_size (path_ptr)); | |
416 | strcpy (orig_path, path_ptr); | |
417 | cygwin_posix_to_win32_path_list (path_ptr, new_path); | |
418 | setenv ("PATH", new_path, 1); | |
419 | } | |
420 | cygwin_conv_to_win32_path (program, real_path); | |
421 | program = real_path; | |
422 | #endif | |
423 | ||
ed50f18f | 424 | argslen = 1; |
b80864fb DJ |
425 | for (argc = 1; program_args[argc]; argc++) |
426 | argslen += strlen (program_args[argc]) + 1; | |
427 | args = alloca (argslen); | |
ed50f18f | 428 | args[0] = '\0'; |
b80864fb DJ |
429 | for (argc = 1; program_args[argc]; argc++) |
430 | { | |
431 | /* FIXME: Can we do better about quoting? How does Cygwin | |
432 | handle this? */ | |
433 | strcat (args, " "); | |
434 | strcat (args, program_args[argc]); | |
435 | } | |
ed50f18f | 436 | OUTMSG2 (("Command line is \"%s\"\n", args)); |
b80864fb | 437 | |
ed50f18f | 438 | #ifdef CREATE_NEW_PROCESS_GROUP |
b80864fb | 439 | flags |= CREATE_NEW_PROCESS_GROUP; |
ed50f18f | 440 | #endif |
b80864fb | 441 | |
ed50f18f PA |
442 | #ifdef __MINGW32CE__ |
443 | to_back_slashes (program); | |
444 | wargs = alloca (argslen * sizeof (wchar_t)); | |
445 | mbstowcs (wargs, args, argslen); | |
446 | wprogram = alloca ((strlen (program) + 1) * sizeof (wchar_t)); | |
447 | mbstowcs (wprogram, program, strlen (program) + 1); | |
448 | ret = CreateProcessW (wprogram, /* image name */ | |
449 | wargs, /* command line */ | |
450 | NULL, /* security, not supported */ | |
451 | NULL, /* thread, not supported */ | |
452 | FALSE, /* inherit handles, not supported */ | |
453 | flags, /* start flags */ | |
454 | NULL, /* environment, not supported */ | |
455 | NULL, /* current directory, not supported */ | |
456 | NULL, /* start info, not supported */ | |
457 | &pi); /* proc info */ | |
458 | #else | |
bf914831 PA |
459 | ret = CreateProcessA (program, /* image name */ |
460 | args, /* command line */ | |
461 | NULL, /* security */ | |
462 | NULL, /* thread */ | |
463 | TRUE, /* inherit handles */ | |
464 | flags, /* start flags */ | |
465 | winenv, /* environment */ | |
466 | NULL, /* current directory */ | |
467 | &si, /* start info */ | |
468 | &pi); /* proc info */ | |
ed50f18f | 469 | #endif |
b80864fb DJ |
470 | |
471 | #ifndef USE_WIN32API | |
472 | if (orig_path) | |
473 | setenv ("PATH", orig_path, 1); | |
474 | #endif | |
475 | ||
476 | if (!ret) | |
477 | { | |
ed50f18f PA |
478 | DWORD err = GetLastError (); |
479 | error ("Error creating process \"%s%s\", (error %d): %s\n", | |
480 | program, args, (int) err, strwinerror (err)); | |
b80864fb DJ |
481 | } |
482 | else | |
483 | { | |
484 | OUTMSG2 (("Process created: %s\n", (char *) args)); | |
485 | } | |
486 | ||
ed50f18f PA |
487 | #ifndef _WIN32_WCE |
488 | /* On Windows CE this handle can't be closed. The OS reuses | |
489 | it in the debug events, while the 9x/NT versions of Windows | |
490 | probably use a DuplicateHandle'd one. */ | |
b80864fb | 491 | CloseHandle (pi.hThread); |
ed50f18f | 492 | #endif |
b80864fb DJ |
493 | |
494 | current_process_handle = pi.hProcess; | |
495 | current_process_id = pi.dwProcessId; | |
496 | ||
497 | do_initial_child_stuff (current_process_id); | |
498 | ||
499 | return current_process_id; | |
500 | } | |
501 | ||
502 | /* Attach to a running process. | |
503 | PID is the process ID to attach to, specified by the user | |
504 | or a higher layer. */ | |
505 | static int | |
506 | win32_attach (unsigned long pid) | |
507 | { | |
bf914831 PA |
508 | winapi_DebugActiveProcessStop DebugActiveProcessStop = NULL; |
509 | winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit = NULL; | |
ed50f18f PA |
510 | #ifdef _WIN32_WCE |
511 | HMODULE dll = GetModuleHandle (_T("COREDLL.DLL")); | |
512 | #else | |
513 | HMODULE dll = GetModuleHandle (_T("KERNEL32.DLL")); | |
514 | #endif | |
bf914831 PA |
515 | DebugActiveProcessStop = GETPROCADDRESS (dll, DebugActiveProcessStop); |
516 | DebugSetProcessKillOnExit = GETPROCADDRESS (dll, DebugSetProcessKillOnExit); | |
b80864fb | 517 | |
1d5315fe PA |
518 | if (DebugActiveProcess (pid)) |
519 | { | |
520 | if (DebugSetProcessKillOnExit != NULL) | |
521 | DebugSetProcessKillOnExit (FALSE); | |
b80864fb | 522 | |
1d5315fe | 523 | current_process_handle = OpenProcess (PROCESS_ALL_ACCESS, FALSE, pid); |
b80864fb | 524 | |
1d5315fe PA |
525 | if (current_process_handle != NULL) |
526 | { | |
527 | current_process_id = pid; | |
528 | do_initial_child_stuff (pid); | |
529 | return 0; | |
530 | } | |
b80864fb DJ |
531 | if (DebugActiveProcessStop != NULL) |
532 | DebugActiveProcessStop (current_process_id); | |
533 | } | |
534 | ||
1d5315fe | 535 | error ("Attach to process failed."); |
b80864fb DJ |
536 | } |
537 | ||
bce7165d PA |
538 | /* Handle OUTPUT_DEBUG_STRING_EVENT from child process. */ |
539 | static void | |
540 | handle_output_debug_string (struct target_waitstatus *ourstatus) | |
541 | { | |
542 | #define READ_BUFFER_LEN 1024 | |
543 | CORE_ADDR addr; | |
544 | char s[READ_BUFFER_LEN + 1] = { 0 }; | |
545 | DWORD nbytes = current_event.u.DebugString.nDebugStringLength; | |
546 | ||
547 | if (nbytes == 0) | |
548 | return; | |
549 | ||
550 | if (nbytes > READ_BUFFER_LEN) | |
551 | nbytes = READ_BUFFER_LEN; | |
552 | ||
553 | addr = (CORE_ADDR) (size_t) current_event.u.DebugString.lpDebugStringData; | |
554 | ||
555 | if (current_event.u.DebugString.fUnicode) | |
556 | { | |
557 | /* The event tells us how many bytes, not chars, even | |
558 | in Unicode. */ | |
559 | WCHAR buffer[(READ_BUFFER_LEN + 1) / sizeof (WCHAR)] = { 0 }; | |
560 | if (read_inferior_memory (addr, (unsigned char *) buffer, nbytes) != 0) | |
561 | return; | |
562 | wcstombs (s, buffer, (nbytes + 1) / sizeof (WCHAR)); | |
563 | } | |
564 | else | |
565 | { | |
566 | if (read_inferior_memory (addr, (unsigned char *) s, nbytes) != 0) | |
567 | return; | |
568 | } | |
569 | ||
570 | if (strncmp (s, "cYg", 3) != 0) | |
45e2715e PA |
571 | { |
572 | if (!server_waiting) | |
573 | { | |
574 | OUTMSG2(("%s", s)); | |
575 | return; | |
576 | } | |
577 | ||
578 | monitor_output (s); | |
579 | } | |
bce7165d PA |
580 | #undef READ_BUFFER_LEN |
581 | } | |
582 | ||
b80864fb DJ |
583 | /* Kill all inferiors. */ |
584 | static void | |
585 | win32_kill (void) | |
586 | { | |
ed50f18f PA |
587 | win32_thread_info *current_thread; |
588 | ||
9d606399 DJ |
589 | if (current_process_handle == NULL) |
590 | return; | |
591 | ||
b80864fb DJ |
592 | TerminateProcess (current_process_handle, 0); |
593 | for (;;) | |
594 | { | |
595 | if (!child_continue (DBG_CONTINUE, -1)) | |
596 | break; | |
597 | if (!WaitForDebugEvent (¤t_event, INFINITE)) | |
598 | break; | |
599 | if (current_event.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT) | |
600 | break; | |
bce7165d PA |
601 | else if (current_event.dwDebugEventCode == OUTPUT_DEBUG_STRING_EVENT) |
602 | { | |
603 | struct target_waitstatus our_status = { 0 }; | |
604 | handle_output_debug_string (&our_status); | |
605 | } | |
b80864fb | 606 | } |
ed50f18f PA |
607 | |
608 | CloseHandle (current_process_handle); | |
609 | ||
610 | current_thread = inferior_target_data (current_inferior); | |
611 | if (current_thread && current_thread->h) | |
612 | { | |
613 | /* This may fail in an attached process, so don't check. */ | |
614 | (void) CloseHandle (current_thread->h); | |
615 | } | |
b80864fb DJ |
616 | } |
617 | ||
618 | /* Detach from all inferiors. */ | |
444d6139 | 619 | static int |
b80864fb DJ |
620 | win32_detach (void) |
621 | { | |
444d6139 PA |
622 | HANDLE h; |
623 | ||
bf914831 PA |
624 | winapi_DebugActiveProcessStop DebugActiveProcessStop = NULL; |
625 | winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit = NULL; | |
ed50f18f PA |
626 | #ifdef _WIN32_WCE |
627 | HMODULE dll = GetModuleHandle (_T("COREDLL.DLL")); | |
628 | #else | |
629 | HMODULE dll = GetModuleHandle (_T("KERNEL32.DLL")); | |
630 | #endif | |
bf914831 PA |
631 | DebugActiveProcessStop = GETPROCADDRESS (dll, DebugActiveProcessStop); |
632 | DebugSetProcessKillOnExit = GETPROCADDRESS (dll, DebugSetProcessKillOnExit); | |
b80864fb | 633 | |
444d6139 PA |
634 | if (DebugSetProcessKillOnExit == NULL |
635 | || DebugActiveProcessStop == NULL) | |
636 | return -1; | |
b80864fb | 637 | |
444d6139 PA |
638 | /* We need a new handle, since DebugActiveProcessStop |
639 | closes all the ones that came through the events. */ | |
640 | if ((h = OpenProcess (PROCESS_ALL_ACCESS, | |
641 | FALSE, | |
642 | current_process_id)) == NULL) | |
643 | { | |
644 | /* The process died. */ | |
645 | return -1; | |
646 | } | |
647 | ||
648 | { | |
649 | struct thread_resume resume; | |
650 | resume.thread = -1; | |
651 | resume.step = 0; | |
652 | resume.sig = 0; | |
653 | resume.leave_stopped = 0; | |
654 | win32_resume (&resume); | |
655 | } | |
656 | ||
657 | if (!DebugActiveProcessStop (current_process_id)) | |
658 | { | |
659 | CloseHandle (h); | |
660 | return -1; | |
661 | } | |
662 | DebugSetProcessKillOnExit (FALSE); | |
663 | ||
664 | current_process_handle = h; | |
665 | return 0; | |
666 | } | |
667 | ||
668 | /* Wait for inferiors to end. */ | |
669 | static void | |
670 | win32_join (void) | |
671 | { | |
672 | if (current_process_id == 0 | |
673 | || current_process_handle == NULL) | |
674 | return; | |
675 | ||
676 | WaitForSingleObject (current_process_handle, INFINITE); | |
677 | CloseHandle (current_process_handle); | |
678 | ||
679 | current_process_handle = NULL; | |
680 | current_process_id = 0; | |
b80864fb DJ |
681 | } |
682 | ||
683 | /* Return 1 iff the thread with thread ID TID is alive. */ | |
684 | static int | |
685 | win32_thread_alive (unsigned long tid) | |
686 | { | |
687 | int res; | |
688 | ||
689 | /* Our thread list is reliable; don't bother to poll target | |
690 | threads. */ | |
691 | if (find_inferior_id (&all_threads, tid) != NULL) | |
692 | res = 1; | |
693 | else | |
694 | res = 0; | |
695 | return res; | |
696 | } | |
697 | ||
698 | /* Resume the inferior process. RESUME_INFO describes how we want | |
699 | to resume. */ | |
700 | static void | |
701 | win32_resume (struct thread_resume *resume_info) | |
702 | { | |
703 | DWORD tid; | |
704 | enum target_signal sig; | |
705 | int step; | |
41093d81 | 706 | win32_thread_info *th; |
b80864fb DJ |
707 | DWORD continue_status = DBG_CONTINUE; |
708 | ||
709 | /* This handles the very limited set of resume packets that GDB can | |
710 | currently produce. */ | |
711 | ||
712 | if (resume_info[0].thread == -1) | |
713 | tid = -1; | |
714 | else if (resume_info[1].thread == -1 && !resume_info[1].leave_stopped) | |
715 | tid = -1; | |
716 | else | |
717 | /* Yes, we're ignoring resume_info[0].thread. It'd be tricky to make | |
718 | the Windows resume code do the right thing for thread switching. */ | |
719 | tid = current_event.dwThreadId; | |
720 | ||
721 | if (resume_info[0].thread != -1) | |
722 | { | |
723 | sig = resume_info[0].sig; | |
724 | step = resume_info[0].step; | |
725 | } | |
726 | else | |
727 | { | |
728 | sig = 0; | |
729 | step = 0; | |
730 | } | |
731 | ||
732 | if (sig != TARGET_SIGNAL_0) | |
733 | { | |
734 | if (current_event.dwDebugEventCode != EXCEPTION_DEBUG_EVENT) | |
735 | { | |
736 | OUTMSG (("Cannot continue with signal %d here.\n", sig)); | |
737 | } | |
738 | else if (sig == last_sig) | |
739 | continue_status = DBG_EXCEPTION_NOT_HANDLED; | |
740 | else | |
741 | OUTMSG (("Can only continue with recieved signal %d.\n", last_sig)); | |
742 | } | |
743 | ||
744 | last_sig = TARGET_SIGNAL_0; | |
745 | ||
746 | /* Get context for the currently selected thread. */ | |
747 | th = thread_rec (current_event.dwThreadId, FALSE); | |
748 | if (th) | |
749 | { | |
750 | if (th->context.ContextFlags) | |
751 | { | |
b80864fb DJ |
752 | /* Move register values from the inferior into the thread |
753 | context structure. */ | |
754 | regcache_invalidate (); | |
755 | ||
756 | if (step) | |
ed50f18f PA |
757 | { |
758 | if (the_low_target.single_step != NULL) | |
759 | (*the_low_target.single_step) (th); | |
760 | else | |
761 | error ("Single stepping is not supported " | |
762 | "in this configuration.\n"); | |
763 | } | |
34b34921 PA |
764 | |
765 | (*the_low_target.set_thread_context) (th, ¤t_event); | |
b80864fb DJ |
766 | th->context.ContextFlags = 0; |
767 | } | |
768 | } | |
769 | ||
770 | /* Allow continuing with the same signal that interrupted us. | |
771 | Otherwise complain. */ | |
772 | ||
773 | child_continue (continue_status, tid); | |
774 | } | |
775 | ||
255e7678 DJ |
776 | static void |
777 | win32_add_one_solib (const char *name, CORE_ADDR load_addr) | |
778 | { | |
779 | char buf[MAX_PATH + 1]; | |
780 | char buf2[MAX_PATH + 1]; | |
781 | ||
782 | #ifdef _WIN32_WCE | |
783 | WIN32_FIND_DATA w32_fd; | |
784 | WCHAR wname[MAX_PATH + 1]; | |
785 | mbstowcs (wname, name, MAX_PATH); | |
786 | HANDLE h = FindFirstFile (wname, &w32_fd); | |
787 | #else | |
788 | WIN32_FIND_DATAA w32_fd; | |
789 | HANDLE h = FindFirstFileA (name, &w32_fd); | |
790 | #endif | |
791 | ||
792 | if (h == INVALID_HANDLE_VALUE) | |
793 | strcpy (buf, name); | |
794 | else | |
795 | { | |
796 | FindClose (h); | |
797 | strcpy (buf, name); | |
798 | #ifndef _WIN32_WCE | |
799 | { | |
800 | char cwd[MAX_PATH + 1]; | |
801 | char *p; | |
802 | if (GetCurrentDirectoryA (MAX_PATH + 1, cwd)) | |
803 | { | |
804 | p = strrchr (buf, '\\'); | |
805 | if (p) | |
806 | p[1] = '\0'; | |
807 | SetCurrentDirectoryA (buf); | |
808 | GetFullPathNameA (w32_fd.cFileName, MAX_PATH, buf, &p); | |
809 | SetCurrentDirectoryA (cwd); | |
810 | } | |
811 | } | |
812 | #endif | |
813 | } | |
814 | ||
815 | #ifdef __CYGWIN__ | |
816 | cygwin_conv_to_posix_path (buf, buf2); | |
817 | #else | |
818 | strcpy (buf2, buf); | |
819 | #endif | |
820 | ||
821 | loaded_dll (buf2, load_addr); | |
822 | } | |
823 | ||
824 | static char * | |
825 | get_image_name (HANDLE h, void *address, int unicode) | |
826 | { | |
827 | static char buf[(2 * MAX_PATH) + 1]; | |
828 | DWORD size = unicode ? sizeof (WCHAR) : sizeof (char); | |
829 | char *address_ptr; | |
830 | int len = 0; | |
831 | char b[2]; | |
832 | DWORD done; | |
833 | ||
834 | /* Attempt to read the name of the dll that was detected. | |
835 | This is documented to work only when actively debugging | |
836 | a program. It will not work for attached processes. */ | |
837 | if (address == NULL) | |
838 | return NULL; | |
839 | ||
840 | #ifdef _WIN32_WCE | |
841 | /* Windows CE reports the address of the image name, | |
842 | instead of an address of a pointer into the image name. */ | |
843 | address_ptr = address; | |
844 | #else | |
845 | /* See if we could read the address of a string, and that the | |
846 | address isn't null. */ | |
847 | if (!ReadProcessMemory (h, address, &address_ptr, | |
848 | sizeof (address_ptr), &done) | |
849 | || done != sizeof (address_ptr) | |
850 | || !address_ptr) | |
851 | return NULL; | |
852 | #endif | |
853 | ||
854 | /* Find the length of the string */ | |
855 | while (ReadProcessMemory (h, address_ptr + len++ * size, &b, size, &done) | |
856 | && (b[0] != 0 || b[size - 1] != 0) && done == size) | |
857 | continue; | |
858 | ||
859 | if (!unicode) | |
860 | ReadProcessMemory (h, address_ptr, buf, len, &done); | |
861 | else | |
862 | { | |
863 | WCHAR *unicode_address = (WCHAR *) alloca (len * sizeof (WCHAR)); | |
864 | ReadProcessMemory (h, address_ptr, unicode_address, len * sizeof (WCHAR), | |
865 | &done); | |
866 | ||
867 | WideCharToMultiByte (CP_ACP, 0, unicode_address, len, buf, len, 0, 0); | |
868 | } | |
869 | ||
870 | return buf; | |
871 | } | |
872 | ||
873 | typedef BOOL (WINAPI *winapi_EnumProcessModules) (HANDLE, HMODULE *, | |
874 | DWORD, LPDWORD); | |
875 | typedef BOOL (WINAPI *winapi_GetModuleInformation) (HANDLE, HMODULE, | |
876 | LPMODULEINFO, DWORD); | |
877 | typedef DWORD (WINAPI *winapi_GetModuleFileNameExA) (HANDLE, HMODULE, | |
878 | LPSTR, DWORD); | |
879 | ||
880 | static winapi_EnumProcessModules win32_EnumProcessModules; | |
881 | static winapi_GetModuleInformation win32_GetModuleInformation; | |
882 | static winapi_GetModuleFileNameExA win32_GetModuleFileNameExA; | |
883 | ||
884 | static BOOL | |
885 | load_psapi (void) | |
886 | { | |
887 | static int psapi_loaded = 0; | |
888 | static HMODULE dll = NULL; | |
889 | ||
890 | if (!psapi_loaded) | |
891 | { | |
892 | psapi_loaded = 1; | |
893 | dll = LoadLibrary (TEXT("psapi.dll")); | |
894 | if (!dll) | |
895 | return FALSE; | |
896 | win32_EnumProcessModules = | |
897 | GETPROCADDRESS (dll, EnumProcessModules); | |
898 | win32_GetModuleInformation = | |
899 | GETPROCADDRESS (dll, GetModuleInformation); | |
900 | win32_GetModuleFileNameExA = | |
901 | GETPROCADDRESS (dll, GetModuleFileNameExA); | |
902 | } | |
903 | ||
904 | return (win32_EnumProcessModules != NULL | |
905 | && win32_GetModuleInformation != NULL | |
906 | && win32_GetModuleFileNameExA != NULL); | |
907 | } | |
908 | ||
909 | static int | |
910 | psapi_get_dll_name (DWORD BaseAddress, char *dll_name_ret) | |
911 | { | |
912 | DWORD len; | |
913 | MODULEINFO mi; | |
914 | size_t i; | |
915 | HMODULE dh_buf[1]; | |
916 | HMODULE *DllHandle = dh_buf; | |
917 | DWORD cbNeeded; | |
918 | BOOL ok; | |
919 | ||
920 | if (!load_psapi ()) | |
921 | goto failed; | |
922 | ||
923 | cbNeeded = 0; | |
924 | ok = (*win32_EnumProcessModules) (current_process_handle, | |
925 | DllHandle, | |
926 | sizeof (HMODULE), | |
927 | &cbNeeded); | |
928 | ||
929 | if (!ok || !cbNeeded) | |
930 | goto failed; | |
931 | ||
932 | DllHandle = (HMODULE *) alloca (cbNeeded); | |
933 | if (!DllHandle) | |
934 | goto failed; | |
935 | ||
936 | ok = (*win32_EnumProcessModules) (current_process_handle, | |
937 | DllHandle, | |
938 | cbNeeded, | |
939 | &cbNeeded); | |
940 | if (!ok) | |
941 | goto failed; | |
942 | ||
943 | for (i = 0; i < ((size_t) cbNeeded / sizeof (HMODULE)); i++) | |
944 | { | |
945 | if (!(*win32_GetModuleInformation) (current_process_handle, | |
946 | DllHandle[i], | |
947 | &mi, | |
948 | sizeof (mi))) | |
949 | { | |
950 | DWORD err = GetLastError (); | |
951 | error ("Can't get module info: (error %d): %s\n", | |
952 | (int) err, strwinerror (err)); | |
953 | } | |
954 | ||
955 | if ((DWORD) (mi.lpBaseOfDll) == BaseAddress) | |
956 | { | |
957 | len = (*win32_GetModuleFileNameExA) (current_process_handle, | |
958 | DllHandle[i], | |
959 | dll_name_ret, | |
960 | MAX_PATH); | |
961 | if (len == 0) | |
962 | { | |
963 | DWORD err = GetLastError (); | |
964 | error ("Error getting dll name: (error %d): %s\n", | |
965 | (int) err, strwinerror (err)); | |
966 | } | |
967 | return 1; | |
968 | } | |
969 | } | |
970 | ||
971 | failed: | |
972 | dll_name_ret[0] = '\0'; | |
973 | return 0; | |
974 | } | |
975 | ||
976 | typedef HANDLE (WINAPI *winapi_CreateToolhelp32Snapshot) (DWORD, DWORD); | |
977 | typedef BOOL (WINAPI *winapi_Module32First) (HANDLE, LPMODULEENTRY32); | |
978 | typedef BOOL (WINAPI *winapi_Module32Next) (HANDLE, LPMODULEENTRY32); | |
979 | ||
980 | static winapi_CreateToolhelp32Snapshot win32_CreateToolhelp32Snapshot; | |
981 | static winapi_Module32First win32_Module32First; | |
982 | static winapi_Module32Next win32_Module32Next; | |
983 | ||
984 | static BOOL | |
985 | load_toolhelp (void) | |
986 | { | |
987 | static int toolhelp_loaded = 0; | |
988 | static HMODULE dll = NULL; | |
989 | ||
990 | if (!toolhelp_loaded) | |
991 | { | |
992 | toolhelp_loaded = 1; | |
993 | #ifndef _WIN32_WCE | |
994 | dll = GetModuleHandle (_T("KERNEL32.DLL")); | |
995 | #else | |
996 | dll = GetModuleHandle (_T("COREDLL.DLL")); | |
997 | #endif | |
998 | if (!dll) | |
999 | return FALSE; | |
1000 | ||
1001 | win32_CreateToolhelp32Snapshot = | |
1002 | GETPROCADDRESS (dll, CreateToolhelp32Snapshot); | |
1003 | win32_Module32First = GETPROCADDRESS (dll, Module32First); | |
1004 | win32_Module32Next = GETPROCADDRESS (dll, Module32Next); | |
1005 | } | |
1006 | ||
1007 | return (win32_CreateToolhelp32Snapshot != NULL | |
1008 | && win32_Module32First != NULL | |
1009 | && win32_Module32Next != NULL); | |
1010 | } | |
1011 | ||
1012 | static int | |
1013 | toolhelp_get_dll_name (DWORD BaseAddress, char *dll_name_ret) | |
1014 | { | |
1015 | HANDLE snapshot_module; | |
1016 | MODULEENTRY32 modEntry = { sizeof (MODULEENTRY32) }; | |
1017 | ||
1018 | if (!load_toolhelp ()) | |
1019 | return 0; | |
1020 | ||
1021 | snapshot_module = win32_CreateToolhelp32Snapshot (TH32CS_SNAPMODULE, | |
1022 | current_event.dwProcessId); | |
1023 | if (snapshot_module == INVALID_HANDLE_VALUE) | |
1024 | return 0; | |
1025 | ||
1026 | /* Ignore the first module, which is the exe. */ | |
1027 | if (!win32_Module32First (snapshot_module, &modEntry)) | |
1028 | goto failed; | |
1029 | ||
1030 | while (win32_Module32Next (snapshot_module, &modEntry)) | |
1031 | if ((DWORD) modEntry.modBaseAddr == BaseAddress) | |
1032 | { | |
1033 | #ifdef UNICODE | |
1034 | wcstombs (dll_name_ret, modEntry.szExePath, MAX_PATH + 1); | |
1035 | #else | |
1036 | strcpy (dll_name_ret, modEntry.szExePath); | |
1037 | #endif | |
1038 | CloseHandle (snapshot_module); | |
1039 | return 1; | |
1040 | } | |
1041 | ||
1042 | failed: | |
1043 | CloseHandle (snapshot_module); | |
1044 | return 0; | |
1045 | } | |
1046 | ||
1047 | static void | |
1048 | handle_load_dll (void) | |
1049 | { | |
1050 | LOAD_DLL_DEBUG_INFO *event = ¤t_event.u.LoadDll; | |
1051 | char dll_buf[MAX_PATH + 1]; | |
1052 | char *dll_name = NULL; | |
1053 | DWORD load_addr; | |
1054 | ||
1055 | dll_buf[0] = dll_buf[sizeof (dll_buf) - 1] = '\0'; | |
1056 | ||
1057 | if (!psapi_get_dll_name ((DWORD) (event->lpBaseOfDll), dll_buf) | |
1058 | && !toolhelp_get_dll_name ((DWORD) (event->lpBaseOfDll), dll_buf)) | |
1059 | dll_buf[0] = dll_buf[sizeof (dll_buf) - 1] = '\0'; | |
1060 | ||
1061 | dll_name = dll_buf; | |
1062 | ||
1063 | if (*dll_name == '\0') | |
1064 | dll_name = get_image_name (current_process_handle, | |
1065 | event->lpImageName, event->fUnicode); | |
1066 | if (!dll_name) | |
1067 | return; | |
1068 | ||
1069 | /* The symbols in a dll are offset by 0x1000, which is the | |
1070 | the offset from 0 of the first byte in an image - because | |
1071 | of the file header and the section alignment. */ | |
1072 | ||
1073 | load_addr = (DWORD) event->lpBaseOfDll + 0x1000; | |
1074 | win32_add_one_solib (dll_name, load_addr); | |
1075 | } | |
1076 | ||
1077 | static void | |
1078 | handle_unload_dll (void) | |
1079 | { | |
1080 | CORE_ADDR load_addr = | |
1081 | (CORE_ADDR) (DWORD) current_event.u.UnloadDll.lpBaseOfDll; | |
1082 | load_addr += 0x1000; | |
1083 | unloaded_dll (NULL, load_addr); | |
1084 | } | |
1085 | ||
34b34921 | 1086 | static void |
b80864fb DJ |
1087 | handle_exception (struct target_waitstatus *ourstatus) |
1088 | { | |
b80864fb DJ |
1089 | DWORD code = current_event.u.Exception.ExceptionRecord.ExceptionCode; |
1090 | ||
1091 | ourstatus->kind = TARGET_WAITKIND_STOPPED; | |
1092 | ||
b80864fb DJ |
1093 | switch (code) |
1094 | { | |
1095 | case EXCEPTION_ACCESS_VIOLATION: | |
1096 | OUTMSG2 (("EXCEPTION_ACCESS_VIOLATION")); | |
1097 | ourstatus->value.sig = TARGET_SIGNAL_SEGV; | |
1098 | break; | |
1099 | case STATUS_STACK_OVERFLOW: | |
1100 | OUTMSG2 (("STATUS_STACK_OVERFLOW")); | |
1101 | ourstatus->value.sig = TARGET_SIGNAL_SEGV; | |
1102 | break; | |
1103 | case STATUS_FLOAT_DENORMAL_OPERAND: | |
1104 | OUTMSG2 (("STATUS_FLOAT_DENORMAL_OPERAND")); | |
1105 | ourstatus->value.sig = TARGET_SIGNAL_FPE; | |
1106 | break; | |
1107 | case EXCEPTION_ARRAY_BOUNDS_EXCEEDED: | |
1108 | OUTMSG2 (("EXCEPTION_ARRAY_BOUNDS_EXCEEDED")); | |
1109 | ourstatus->value.sig = TARGET_SIGNAL_FPE; | |
1110 | break; | |
1111 | case STATUS_FLOAT_INEXACT_RESULT: | |
1112 | OUTMSG2 (("STATUS_FLOAT_INEXACT_RESULT")); | |
1113 | ourstatus->value.sig = TARGET_SIGNAL_FPE; | |
1114 | break; | |
1115 | case STATUS_FLOAT_INVALID_OPERATION: | |
1116 | OUTMSG2 (("STATUS_FLOAT_INVALID_OPERATION")); | |
1117 | ourstatus->value.sig = TARGET_SIGNAL_FPE; | |
1118 | break; | |
1119 | case STATUS_FLOAT_OVERFLOW: | |
1120 | OUTMSG2 (("STATUS_FLOAT_OVERFLOW")); | |
1121 | ourstatus->value.sig = TARGET_SIGNAL_FPE; | |
1122 | break; | |
1123 | case STATUS_FLOAT_STACK_CHECK: | |
1124 | OUTMSG2 (("STATUS_FLOAT_STACK_CHECK")); | |
1125 | ourstatus->value.sig = TARGET_SIGNAL_FPE; | |
1126 | break; | |
1127 | case STATUS_FLOAT_UNDERFLOW: | |
1128 | OUTMSG2 (("STATUS_FLOAT_UNDERFLOW")); | |
1129 | ourstatus->value.sig = TARGET_SIGNAL_FPE; | |
1130 | break; | |
1131 | case STATUS_FLOAT_DIVIDE_BY_ZERO: | |
1132 | OUTMSG2 (("STATUS_FLOAT_DIVIDE_BY_ZERO")); | |
1133 | ourstatus->value.sig = TARGET_SIGNAL_FPE; | |
1134 | break; | |
1135 | case STATUS_INTEGER_DIVIDE_BY_ZERO: | |
1136 | OUTMSG2 (("STATUS_INTEGER_DIVIDE_BY_ZERO")); | |
1137 | ourstatus->value.sig = TARGET_SIGNAL_FPE; | |
1138 | break; | |
1139 | case STATUS_INTEGER_OVERFLOW: | |
1140 | OUTMSG2 (("STATUS_INTEGER_OVERFLOW")); | |
1141 | ourstatus->value.sig = TARGET_SIGNAL_FPE; | |
1142 | break; | |
1143 | case EXCEPTION_BREAKPOINT: | |
1144 | OUTMSG2 (("EXCEPTION_BREAKPOINT")); | |
1145 | ourstatus->value.sig = TARGET_SIGNAL_TRAP; | |
ed50f18f PA |
1146 | #ifdef _WIN32_WCE |
1147 | /* Remove the initial breakpoint. */ | |
1148 | check_breakpoints ((CORE_ADDR) (long) current_event | |
1149 | .u.Exception.ExceptionRecord.ExceptionAddress); | |
1150 | #endif | |
b80864fb DJ |
1151 | break; |
1152 | case DBG_CONTROL_C: | |
1153 | OUTMSG2 (("DBG_CONTROL_C")); | |
1154 | ourstatus->value.sig = TARGET_SIGNAL_INT; | |
1155 | break; | |
1156 | case DBG_CONTROL_BREAK: | |
1157 | OUTMSG2 (("DBG_CONTROL_BREAK")); | |
1158 | ourstatus->value.sig = TARGET_SIGNAL_INT; | |
1159 | break; | |
1160 | case EXCEPTION_SINGLE_STEP: | |
1161 | OUTMSG2 (("EXCEPTION_SINGLE_STEP")); | |
1162 | ourstatus->value.sig = TARGET_SIGNAL_TRAP; | |
1163 | break; | |
1164 | case EXCEPTION_ILLEGAL_INSTRUCTION: | |
1165 | OUTMSG2 (("EXCEPTION_ILLEGAL_INSTRUCTION")); | |
1166 | ourstatus->value.sig = TARGET_SIGNAL_ILL; | |
1167 | break; | |
1168 | case EXCEPTION_PRIV_INSTRUCTION: | |
1169 | OUTMSG2 (("EXCEPTION_PRIV_INSTRUCTION")); | |
1170 | ourstatus->value.sig = TARGET_SIGNAL_ILL; | |
1171 | break; | |
1172 | case EXCEPTION_NONCONTINUABLE_EXCEPTION: | |
1173 | OUTMSG2 (("EXCEPTION_NONCONTINUABLE_EXCEPTION")); | |
1174 | ourstatus->value.sig = TARGET_SIGNAL_ILL; | |
1175 | break; | |
1176 | default: | |
1177 | if (current_event.u.Exception.dwFirstChance) | |
34b34921 PA |
1178 | { |
1179 | ourstatus->kind = TARGET_WAITKIND_SPURIOUS; | |
1180 | return; | |
1181 | } | |
b80864fb DJ |
1182 | OUTMSG2 (("gdbserver: unknown target exception 0x%08lx at 0x%08lx", |
1183 | current_event.u.Exception.ExceptionRecord.ExceptionCode, | |
1184 | (DWORD) current_event.u.Exception.ExceptionRecord. | |
1185 | ExceptionAddress)); | |
1186 | ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN; | |
1187 | break; | |
1188 | } | |
1189 | OUTMSG2 (("\n")); | |
1190 | last_sig = ourstatus->value.sig; | |
b80864fb DJ |
1191 | } |
1192 | ||
34b34921 PA |
1193 | /* Get the next event from the child. */ |
1194 | static void | |
b80864fb DJ |
1195 | get_child_debug_event (struct target_waitstatus *ourstatus) |
1196 | { | |
1197 | BOOL debug_event; | |
b80864fb DJ |
1198 | |
1199 | last_sig = TARGET_SIGNAL_0; | |
1200 | ourstatus->kind = TARGET_WAITKIND_SPURIOUS; | |
1201 | ||
7390519e PA |
1202 | /* Keep the wait time low enough for confortable remote interruption, |
1203 | but high enough so gdbserver doesn't become a bottleneck. */ | |
1204 | if (!(debug_event = WaitForDebugEvent (¤t_event, 250))) | |
34b34921 | 1205 | return; |
b80864fb DJ |
1206 | |
1207 | current_inferior = | |
1208 | (struct thread_info *) find_inferior_id (&all_threads, | |
1209 | current_event.dwThreadId); | |
1210 | ||
34b34921 | 1211 | switch (current_event.dwDebugEventCode) |
b80864fb DJ |
1212 | { |
1213 | case CREATE_THREAD_DEBUG_EVENT: | |
1214 | OUTMSG2 (("gdbserver: kernel event CREATE_THREAD_DEBUG_EVENT " | |
1215 | "for pid=%d tid=%x)\n", | |
1216 | (unsigned) current_event.dwProcessId, | |
1217 | (unsigned) current_event.dwThreadId)); | |
1218 | ||
1219 | /* Record the existence of this thread. */ | |
34b34921 | 1220 | child_add_thread (current_event.dwThreadId, |
b80864fb | 1221 | current_event.u.CreateThread.hThread); |
b80864fb DJ |
1222 | break; |
1223 | ||
1224 | case EXIT_THREAD_DEBUG_EVENT: | |
1225 | OUTMSG2 (("gdbserver: kernel event EXIT_THREAD_DEBUG_EVENT " | |
1226 | "for pid=%d tid=%x\n", | |
1227 | (unsigned) current_event.dwProcessId, | |
1228 | (unsigned) current_event.dwThreadId)); | |
1229 | child_delete_thread (current_event.dwThreadId); | |
b80864fb DJ |
1230 | break; |
1231 | ||
1232 | case CREATE_PROCESS_DEBUG_EVENT: | |
1233 | OUTMSG2 (("gdbserver: kernel event CREATE_PROCESS_DEBUG_EVENT " | |
1234 | "for pid=%d tid=%x\n", | |
1235 | (unsigned) current_event.dwProcessId, | |
1236 | (unsigned) current_event.dwThreadId)); | |
1237 | CloseHandle (current_event.u.CreateProcessInfo.hFile); | |
1238 | ||
1239 | current_process_handle = current_event.u.CreateProcessInfo.hProcess; | |
1240 | main_thread_id = current_event.dwThreadId; | |
1241 | ||
1242 | ourstatus->kind = TARGET_WAITKIND_EXECD; | |
1243 | ourstatus->value.execd_pathname = "Main executable"; | |
1244 | ||
1245 | /* Add the main thread. */ | |
34b34921 PA |
1246 | child_add_thread (main_thread_id, |
1247 | current_event.u.CreateProcessInfo.hThread); | |
b80864fb | 1248 | |
34b34921 | 1249 | ourstatus->value.related_pid = current_event.dwThreadId; |
ed50f18f PA |
1250 | #ifdef _WIN32_WCE |
1251 | /* Windows CE doesn't set the initial breakpoint automatically | |
1252 | like the desktop versions of Windows do. We add it explicitly | |
1253 | here. It will be removed as soon as it is hit. */ | |
1254 | set_breakpoint_at ((CORE_ADDR) (long) current_event.u | |
1255 | .CreateProcessInfo.lpStartAddress, | |
1256 | delete_breakpoint_at); | |
1257 | #endif | |
b80864fb DJ |
1258 | break; |
1259 | ||
1260 | case EXIT_PROCESS_DEBUG_EVENT: | |
1261 | OUTMSG2 (("gdbserver: kernel event EXIT_PROCESS_DEBUG_EVENT " | |
1262 | "for pid=%d tid=%x\n", | |
1263 | (unsigned) current_event.dwProcessId, | |
1264 | (unsigned) current_event.dwThreadId)); | |
1265 | ourstatus->kind = TARGET_WAITKIND_EXITED; | |
1266 | ourstatus->value.integer = current_event.u.ExitProcess.dwExitCode; | |
1267 | CloseHandle (current_process_handle); | |
9d606399 | 1268 | current_process_handle = NULL; |
b80864fb DJ |
1269 | break; |
1270 | ||
1271 | case LOAD_DLL_DEBUG_EVENT: | |
1272 | OUTMSG2 (("gdbserver: kernel event LOAD_DLL_DEBUG_EVENT " | |
1273 | "for pid=%d tid=%x\n", | |
1274 | (unsigned) current_event.dwProcessId, | |
1275 | (unsigned) current_event.dwThreadId)); | |
1276 | CloseHandle (current_event.u.LoadDll.hFile); | |
255e7678 | 1277 | handle_load_dll (); |
b80864fb DJ |
1278 | |
1279 | ourstatus->kind = TARGET_WAITKIND_LOADED; | |
255e7678 | 1280 | ourstatus->value.sig = TARGET_SIGNAL_TRAP; |
b80864fb DJ |
1281 | break; |
1282 | ||
1283 | case UNLOAD_DLL_DEBUG_EVENT: | |
1284 | OUTMSG2 (("gdbserver: kernel event UNLOAD_DLL_DEBUG_EVENT " | |
1285 | "for pid=%d tid=%x\n", | |
1286 | (unsigned) current_event.dwProcessId, | |
1287 | (unsigned) current_event.dwThreadId)); | |
255e7678 DJ |
1288 | handle_unload_dll (); |
1289 | ourstatus->kind = TARGET_WAITKIND_LOADED; | |
1290 | ourstatus->value.sig = TARGET_SIGNAL_TRAP; | |
b80864fb DJ |
1291 | break; |
1292 | ||
1293 | case EXCEPTION_DEBUG_EVENT: | |
1294 | OUTMSG2 (("gdbserver: kernel event EXCEPTION_DEBUG_EVENT " | |
1295 | "for pid=%d tid=%x\n", | |
1296 | (unsigned) current_event.dwProcessId, | |
1297 | (unsigned) current_event.dwThreadId)); | |
34b34921 | 1298 | handle_exception (ourstatus); |
b80864fb DJ |
1299 | break; |
1300 | ||
1301 | case OUTPUT_DEBUG_STRING_EVENT: | |
1302 | /* A message from the kernel (or Cygwin). */ | |
1303 | OUTMSG2 (("gdbserver: kernel event OUTPUT_DEBUG_STRING_EVENT " | |
1304 | "for pid=%d tid=%x\n", | |
1305 | (unsigned) current_event.dwProcessId, | |
1306 | (unsigned) current_event.dwThreadId)); | |
bce7165d | 1307 | handle_output_debug_string (ourstatus); |
b80864fb DJ |
1308 | break; |
1309 | ||
1310 | default: | |
1311 | OUTMSG2 (("gdbserver: kernel event unknown " | |
1312 | "for pid=%d tid=%x code=%ld\n", | |
1313 | (unsigned) current_event.dwProcessId, | |
1314 | (unsigned) current_event.dwThreadId, | |
1315 | current_event.dwDebugEventCode)); | |
1316 | break; | |
1317 | } | |
1318 | ||
1319 | current_inferior = | |
1320 | (struct thread_info *) find_inferior_id (&all_threads, | |
1321 | current_event.dwThreadId); | |
b80864fb DJ |
1322 | } |
1323 | ||
1324 | /* Wait for the inferior process to change state. | |
1325 | STATUS will be filled in with a response code to send to GDB. | |
1326 | Returns the signal which caused the process to stop. */ | |
1327 | static unsigned char | |
1328 | win32_wait (char *status) | |
1329 | { | |
1330 | struct target_waitstatus our_status; | |
1331 | ||
1332 | *status = 'T'; | |
1333 | ||
1334 | while (1) | |
1335 | { | |
7390519e PA |
1336 | /* Check if GDB sent us an interrupt request. */ |
1337 | check_remote_input_interrupt_request (); | |
1338 | ||
b80864fb DJ |
1339 | get_child_debug_event (&our_status); |
1340 | ||
34b34921 | 1341 | switch (our_status.kind) |
b80864fb | 1342 | { |
34b34921 | 1343 | case TARGET_WAITKIND_EXITED: |
b80864fb DJ |
1344 | OUTMSG2 (("Child exited with retcode = %x\n", |
1345 | our_status.value.integer)); | |
1346 | ||
1347 | *status = 'W'; | |
1348 | ||
1349 | child_fetch_inferior_registers (-1); | |
1350 | ||
1351 | return our_status.value.integer; | |
34b34921 | 1352 | case TARGET_WAITKIND_STOPPED: |
255e7678 | 1353 | case TARGET_WAITKIND_LOADED: |
f72f3e60 | 1354 | OUTMSG2 (("Child Stopped with signal = %d \n", |
ed50f18f | 1355 | our_status.value.sig)); |
b80864fb DJ |
1356 | |
1357 | *status = 'T'; | |
1358 | ||
1359 | child_fetch_inferior_registers (-1); | |
1360 | ||
255e7678 DJ |
1361 | if (our_status.kind == TARGET_WAITKIND_LOADED |
1362 | && !server_waiting) | |
1363 | { | |
1364 | /* When gdb connects, we want to be stopped at the | |
1365 | initial breakpoint, not in some dll load event. */ | |
1366 | child_continue (DBG_CONTINUE, -1); | |
1367 | break; | |
1368 | } | |
1369 | ||
b80864fb | 1370 | return our_status.value.sig; |
34b34921 PA |
1371 | default: |
1372 | OUTMSG (("Ignoring unknown internal event, %d\n", our_status.kind)); | |
1373 | /* fall-through */ | |
1374 | case TARGET_WAITKIND_SPURIOUS: | |
34b34921 PA |
1375 | case TARGET_WAITKIND_EXECD: |
1376 | /* do nothing, just continue */ | |
1377 | child_continue (DBG_CONTINUE, -1); | |
1378 | break; | |
b80864fb | 1379 | } |
b80864fb DJ |
1380 | } |
1381 | } | |
1382 | ||
1383 | /* Fetch registers from the inferior process. | |
1384 | If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */ | |
1385 | static void | |
1386 | win32_fetch_inferior_registers (int regno) | |
1387 | { | |
1388 | child_fetch_inferior_registers (regno); | |
1389 | } | |
1390 | ||
1391 | /* Store registers to the inferior process. | |
1392 | If REGNO is -1, store all registers; otherwise, store at least REGNO. */ | |
1393 | static void | |
1394 | win32_store_inferior_registers (int regno) | |
1395 | { | |
1396 | child_store_inferior_registers (regno); | |
1397 | } | |
1398 | ||
1399 | /* Read memory from the inferior process. This should generally be | |
1400 | called through read_inferior_memory, which handles breakpoint shadowing. | |
1401 | Read LEN bytes at MEMADDR into a buffer at MYADDR. */ | |
1402 | static int | |
1403 | win32_read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len) | |
1404 | { | |
ed50f18f | 1405 | return child_xfer_memory (memaddr, (char *) myaddr, len, 0, 0) != len; |
b80864fb DJ |
1406 | } |
1407 | ||
1408 | /* Write memory to the inferior process. This should generally be | |
1409 | called through write_inferior_memory, which handles breakpoint shadowing. | |
1410 | Write LEN bytes from the buffer at MYADDR to MEMADDR. | |
1411 | Returns 0 on success and errno on failure. */ | |
1412 | static int | |
1413 | win32_write_inferior_memory (CORE_ADDR memaddr, const unsigned char *myaddr, | |
1414 | int len) | |
1415 | { | |
1416 | return child_xfer_memory (memaddr, (char *) myaddr, len, 1, 0) != len; | |
1417 | } | |
1418 | ||
7390519e PA |
1419 | /* Send an interrupt request to the inferior process. */ |
1420 | static void | |
1421 | win32_request_interrupt (void) | |
1422 | { | |
1423 | winapi_DebugBreakProcess DebugBreakProcess; | |
1424 | winapi_GenerateConsoleCtrlEvent GenerateConsoleCtrlEvent; | |
1425 | ||
1426 | #ifdef _WIN32_WCE | |
1427 | HMODULE dll = GetModuleHandle (_T("COREDLL.DLL")); | |
1428 | #else | |
1429 | HMODULE dll = GetModuleHandle (_T("KERNEL32.DLL")); | |
1430 | #endif | |
1431 | ||
1432 | GenerateConsoleCtrlEvent = GETPROCADDRESS (dll, GenerateConsoleCtrlEvent); | |
1433 | ||
1434 | if (GenerateConsoleCtrlEvent != NULL | |
1435 | && GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT, current_process_id)) | |
1436 | return; | |
1437 | ||
1438 | /* GenerateConsoleCtrlEvent can fail if process id being debugged is | |
1439 | not a process group id. | |
1440 | Fallback to XP/Vista 'DebugBreakProcess', which generates a | |
1441 | breakpoint exception in the interior process. */ | |
1442 | ||
1443 | DebugBreakProcess = GETPROCADDRESS (dll, DebugBreakProcess); | |
1444 | ||
1445 | if (DebugBreakProcess != NULL | |
1446 | && DebugBreakProcess (current_process_handle)) | |
1447 | return; | |
1448 | ||
1449 | OUTMSG (("Could not interrupt process.\n")); | |
1450 | } | |
1451 | ||
820f2bda PA |
1452 | static const char * |
1453 | win32_arch_string (void) | |
1454 | { | |
ed50f18f | 1455 | return the_low_target.arch_string; |
820f2bda PA |
1456 | } |
1457 | ||
b80864fb DJ |
1458 | static struct target_ops win32_target_ops = { |
1459 | win32_create_inferior, | |
1460 | win32_attach, | |
1461 | win32_kill, | |
1462 | win32_detach, | |
444d6139 | 1463 | win32_join, |
b80864fb DJ |
1464 | win32_thread_alive, |
1465 | win32_resume, | |
1466 | win32_wait, | |
1467 | win32_fetch_inferior_registers, | |
1468 | win32_store_inferior_registers, | |
1469 | win32_read_inferior_memory, | |
1470 | win32_write_inferior_memory, | |
820f2bda | 1471 | NULL, |
7390519e | 1472 | win32_request_interrupt, |
820f2bda PA |
1473 | NULL, |
1474 | NULL, | |
1475 | NULL, | |
1476 | NULL, | |
1477 | NULL, | |
1478 | NULL, | |
1479 | NULL, | |
1480 | win32_arch_string | |
b80864fb DJ |
1481 | }; |
1482 | ||
1483 | /* Initialize the Win32 backend. */ | |
1484 | void | |
1485 | initialize_low (void) | |
1486 | { | |
1487 | set_target_ops (&win32_target_ops); | |
ed50f18f PA |
1488 | if (the_low_target.breakpoint != NULL) |
1489 | set_breakpoint_data (the_low_target.breakpoint, | |
1490 | the_low_target.breakpoint_len); | |
b80864fb DJ |
1491 | init_registers (); |
1492 | } |