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; | |
6b3d9b83 PA |
983 | #ifdef _WIN32_WCE |
984 | typedef BOOL (WINAPI *winapi_CloseToolhelp32Snapshot) (HANDLE); | |
985 | static winapi_CloseToolhelp32Snapshot win32_CloseToolhelp32Snapshot; | |
986 | #endif | |
255e7678 DJ |
987 | |
988 | static BOOL | |
989 | load_toolhelp (void) | |
990 | { | |
991 | static int toolhelp_loaded = 0; | |
992 | static HMODULE dll = NULL; | |
993 | ||
994 | if (!toolhelp_loaded) | |
995 | { | |
996 | toolhelp_loaded = 1; | |
997 | #ifndef _WIN32_WCE | |
998 | dll = GetModuleHandle (_T("KERNEL32.DLL")); | |
999 | #else | |
6b3d9b83 | 1000 | dll = LoadLibrary (L"TOOLHELP.DLL"); |
255e7678 DJ |
1001 | #endif |
1002 | if (!dll) | |
1003 | return FALSE; | |
1004 | ||
1005 | win32_CreateToolhelp32Snapshot = | |
1006 | GETPROCADDRESS (dll, CreateToolhelp32Snapshot); | |
1007 | win32_Module32First = GETPROCADDRESS (dll, Module32First); | |
1008 | win32_Module32Next = GETPROCADDRESS (dll, Module32Next); | |
6b3d9b83 PA |
1009 | #ifdef _WIN32_WCE |
1010 | win32_CloseToolhelp32Snapshot = | |
1011 | GETPROCADDRESS (dll, CloseToolhelp32Snapshot); | |
1012 | #endif | |
255e7678 DJ |
1013 | } |
1014 | ||
1015 | return (win32_CreateToolhelp32Snapshot != NULL | |
1016 | && win32_Module32First != NULL | |
6b3d9b83 PA |
1017 | && win32_Module32Next != NULL |
1018 | #ifdef _WIN32_WCE | |
1019 | && win32_CloseToolhelp32Snapshot != NULL | |
1020 | #endif | |
1021 | ); | |
255e7678 DJ |
1022 | } |
1023 | ||
1024 | static int | |
1025 | toolhelp_get_dll_name (DWORD BaseAddress, char *dll_name_ret) | |
1026 | { | |
1027 | HANDLE snapshot_module; | |
1028 | MODULEENTRY32 modEntry = { sizeof (MODULEENTRY32) }; | |
6b3d9b83 | 1029 | int found = 0; |
255e7678 DJ |
1030 | |
1031 | if (!load_toolhelp ()) | |
1032 | return 0; | |
1033 | ||
1034 | snapshot_module = win32_CreateToolhelp32Snapshot (TH32CS_SNAPMODULE, | |
1035 | current_event.dwProcessId); | |
1036 | if (snapshot_module == INVALID_HANDLE_VALUE) | |
1037 | return 0; | |
1038 | ||
1039 | /* Ignore the first module, which is the exe. */ | |
6b3d9b83 PA |
1040 | if (win32_Module32First (snapshot_module, &modEntry)) |
1041 | while (win32_Module32Next (snapshot_module, &modEntry)) | |
1042 | if ((DWORD) modEntry.modBaseAddr == BaseAddress) | |
1043 | { | |
255e7678 | 1044 | #ifdef UNICODE |
6b3d9b83 | 1045 | wcstombs (dll_name_ret, modEntry.szExePath, MAX_PATH + 1); |
255e7678 | 1046 | #else |
6b3d9b83 | 1047 | strcpy (dll_name_ret, modEntry.szExePath); |
255e7678 | 1048 | #endif |
6b3d9b83 PA |
1049 | found = 1; |
1050 | break; | |
1051 | } | |
255e7678 | 1052 | |
6b3d9b83 PA |
1053 | #ifdef _WIN32_WCE |
1054 | win32_CloseToolhelp32Snapshot (snapshot_module); | |
1055 | #else | |
255e7678 | 1056 | CloseHandle (snapshot_module); |
6b3d9b83 PA |
1057 | #endif |
1058 | return found; | |
255e7678 DJ |
1059 | } |
1060 | ||
1061 | static void | |
1062 | handle_load_dll (void) | |
1063 | { | |
1064 | LOAD_DLL_DEBUG_INFO *event = ¤t_event.u.LoadDll; | |
1065 | char dll_buf[MAX_PATH + 1]; | |
1066 | char *dll_name = NULL; | |
1067 | DWORD load_addr; | |
1068 | ||
1069 | dll_buf[0] = dll_buf[sizeof (dll_buf) - 1] = '\0'; | |
1070 | ||
1071 | if (!psapi_get_dll_name ((DWORD) (event->lpBaseOfDll), dll_buf) | |
1072 | && !toolhelp_get_dll_name ((DWORD) (event->lpBaseOfDll), dll_buf)) | |
1073 | dll_buf[0] = dll_buf[sizeof (dll_buf) - 1] = '\0'; | |
1074 | ||
1075 | dll_name = dll_buf; | |
1076 | ||
1077 | if (*dll_name == '\0') | |
1078 | dll_name = get_image_name (current_process_handle, | |
1079 | event->lpImageName, event->fUnicode); | |
1080 | if (!dll_name) | |
1081 | return; | |
1082 | ||
1083 | /* The symbols in a dll are offset by 0x1000, which is the | |
1084 | the offset from 0 of the first byte in an image - because | |
1085 | of the file header and the section alignment. */ | |
1086 | ||
1087 | load_addr = (DWORD) event->lpBaseOfDll + 0x1000; | |
1088 | win32_add_one_solib (dll_name, load_addr); | |
1089 | } | |
1090 | ||
1091 | static void | |
1092 | handle_unload_dll (void) | |
1093 | { | |
1094 | CORE_ADDR load_addr = | |
1095 | (CORE_ADDR) (DWORD) current_event.u.UnloadDll.lpBaseOfDll; | |
1096 | load_addr += 0x1000; | |
1097 | unloaded_dll (NULL, load_addr); | |
1098 | } | |
1099 | ||
34b34921 | 1100 | static void |
b80864fb DJ |
1101 | handle_exception (struct target_waitstatus *ourstatus) |
1102 | { | |
b80864fb DJ |
1103 | DWORD code = current_event.u.Exception.ExceptionRecord.ExceptionCode; |
1104 | ||
1105 | ourstatus->kind = TARGET_WAITKIND_STOPPED; | |
1106 | ||
b80864fb DJ |
1107 | switch (code) |
1108 | { | |
1109 | case EXCEPTION_ACCESS_VIOLATION: | |
1110 | OUTMSG2 (("EXCEPTION_ACCESS_VIOLATION")); | |
1111 | ourstatus->value.sig = TARGET_SIGNAL_SEGV; | |
1112 | break; | |
1113 | case STATUS_STACK_OVERFLOW: | |
1114 | OUTMSG2 (("STATUS_STACK_OVERFLOW")); | |
1115 | ourstatus->value.sig = TARGET_SIGNAL_SEGV; | |
1116 | break; | |
1117 | case STATUS_FLOAT_DENORMAL_OPERAND: | |
1118 | OUTMSG2 (("STATUS_FLOAT_DENORMAL_OPERAND")); | |
1119 | ourstatus->value.sig = TARGET_SIGNAL_FPE; | |
1120 | break; | |
1121 | case EXCEPTION_ARRAY_BOUNDS_EXCEEDED: | |
1122 | OUTMSG2 (("EXCEPTION_ARRAY_BOUNDS_EXCEEDED")); | |
1123 | ourstatus->value.sig = TARGET_SIGNAL_FPE; | |
1124 | break; | |
1125 | case STATUS_FLOAT_INEXACT_RESULT: | |
1126 | OUTMSG2 (("STATUS_FLOAT_INEXACT_RESULT")); | |
1127 | ourstatus->value.sig = TARGET_SIGNAL_FPE; | |
1128 | break; | |
1129 | case STATUS_FLOAT_INVALID_OPERATION: | |
1130 | OUTMSG2 (("STATUS_FLOAT_INVALID_OPERATION")); | |
1131 | ourstatus->value.sig = TARGET_SIGNAL_FPE; | |
1132 | break; | |
1133 | case STATUS_FLOAT_OVERFLOW: | |
1134 | OUTMSG2 (("STATUS_FLOAT_OVERFLOW")); | |
1135 | ourstatus->value.sig = TARGET_SIGNAL_FPE; | |
1136 | break; | |
1137 | case STATUS_FLOAT_STACK_CHECK: | |
1138 | OUTMSG2 (("STATUS_FLOAT_STACK_CHECK")); | |
1139 | ourstatus->value.sig = TARGET_SIGNAL_FPE; | |
1140 | break; | |
1141 | case STATUS_FLOAT_UNDERFLOW: | |
1142 | OUTMSG2 (("STATUS_FLOAT_UNDERFLOW")); | |
1143 | ourstatus->value.sig = TARGET_SIGNAL_FPE; | |
1144 | break; | |
1145 | case STATUS_FLOAT_DIVIDE_BY_ZERO: | |
1146 | OUTMSG2 (("STATUS_FLOAT_DIVIDE_BY_ZERO")); | |
1147 | ourstatus->value.sig = TARGET_SIGNAL_FPE; | |
1148 | break; | |
1149 | case STATUS_INTEGER_DIVIDE_BY_ZERO: | |
1150 | OUTMSG2 (("STATUS_INTEGER_DIVIDE_BY_ZERO")); | |
1151 | ourstatus->value.sig = TARGET_SIGNAL_FPE; | |
1152 | break; | |
1153 | case STATUS_INTEGER_OVERFLOW: | |
1154 | OUTMSG2 (("STATUS_INTEGER_OVERFLOW")); | |
1155 | ourstatus->value.sig = TARGET_SIGNAL_FPE; | |
1156 | break; | |
1157 | case EXCEPTION_BREAKPOINT: | |
1158 | OUTMSG2 (("EXCEPTION_BREAKPOINT")); | |
1159 | ourstatus->value.sig = TARGET_SIGNAL_TRAP; | |
ed50f18f PA |
1160 | #ifdef _WIN32_WCE |
1161 | /* Remove the initial breakpoint. */ | |
1162 | check_breakpoints ((CORE_ADDR) (long) current_event | |
1163 | .u.Exception.ExceptionRecord.ExceptionAddress); | |
1164 | #endif | |
b80864fb DJ |
1165 | break; |
1166 | case DBG_CONTROL_C: | |
1167 | OUTMSG2 (("DBG_CONTROL_C")); | |
1168 | ourstatus->value.sig = TARGET_SIGNAL_INT; | |
1169 | break; | |
1170 | case DBG_CONTROL_BREAK: | |
1171 | OUTMSG2 (("DBG_CONTROL_BREAK")); | |
1172 | ourstatus->value.sig = TARGET_SIGNAL_INT; | |
1173 | break; | |
1174 | case EXCEPTION_SINGLE_STEP: | |
1175 | OUTMSG2 (("EXCEPTION_SINGLE_STEP")); | |
1176 | ourstatus->value.sig = TARGET_SIGNAL_TRAP; | |
1177 | break; | |
1178 | case EXCEPTION_ILLEGAL_INSTRUCTION: | |
1179 | OUTMSG2 (("EXCEPTION_ILLEGAL_INSTRUCTION")); | |
1180 | ourstatus->value.sig = TARGET_SIGNAL_ILL; | |
1181 | break; | |
1182 | case EXCEPTION_PRIV_INSTRUCTION: | |
1183 | OUTMSG2 (("EXCEPTION_PRIV_INSTRUCTION")); | |
1184 | ourstatus->value.sig = TARGET_SIGNAL_ILL; | |
1185 | break; | |
1186 | case EXCEPTION_NONCONTINUABLE_EXCEPTION: | |
1187 | OUTMSG2 (("EXCEPTION_NONCONTINUABLE_EXCEPTION")); | |
1188 | ourstatus->value.sig = TARGET_SIGNAL_ILL; | |
1189 | break; | |
1190 | default: | |
1191 | if (current_event.u.Exception.dwFirstChance) | |
34b34921 PA |
1192 | { |
1193 | ourstatus->kind = TARGET_WAITKIND_SPURIOUS; | |
1194 | return; | |
1195 | } | |
b80864fb DJ |
1196 | OUTMSG2 (("gdbserver: unknown target exception 0x%08lx at 0x%08lx", |
1197 | current_event.u.Exception.ExceptionRecord.ExceptionCode, | |
1198 | (DWORD) current_event.u.Exception.ExceptionRecord. | |
1199 | ExceptionAddress)); | |
1200 | ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN; | |
1201 | break; | |
1202 | } | |
1203 | OUTMSG2 (("\n")); | |
1204 | last_sig = ourstatus->value.sig; | |
b80864fb DJ |
1205 | } |
1206 | ||
34b34921 PA |
1207 | /* Get the next event from the child. */ |
1208 | static void | |
b80864fb DJ |
1209 | get_child_debug_event (struct target_waitstatus *ourstatus) |
1210 | { | |
1211 | BOOL debug_event; | |
b80864fb DJ |
1212 | |
1213 | last_sig = TARGET_SIGNAL_0; | |
1214 | ourstatus->kind = TARGET_WAITKIND_SPURIOUS; | |
1215 | ||
7390519e PA |
1216 | /* Keep the wait time low enough for confortable remote interruption, |
1217 | but high enough so gdbserver doesn't become a bottleneck. */ | |
1218 | if (!(debug_event = WaitForDebugEvent (¤t_event, 250))) | |
34b34921 | 1219 | return; |
b80864fb DJ |
1220 | |
1221 | current_inferior = | |
1222 | (struct thread_info *) find_inferior_id (&all_threads, | |
1223 | current_event.dwThreadId); | |
1224 | ||
34b34921 | 1225 | switch (current_event.dwDebugEventCode) |
b80864fb DJ |
1226 | { |
1227 | case CREATE_THREAD_DEBUG_EVENT: | |
1228 | OUTMSG2 (("gdbserver: kernel event CREATE_THREAD_DEBUG_EVENT " | |
1229 | "for pid=%d tid=%x)\n", | |
1230 | (unsigned) current_event.dwProcessId, | |
1231 | (unsigned) current_event.dwThreadId)); | |
1232 | ||
1233 | /* Record the existence of this thread. */ | |
34b34921 | 1234 | child_add_thread (current_event.dwThreadId, |
b80864fb | 1235 | current_event.u.CreateThread.hThread); |
b80864fb DJ |
1236 | break; |
1237 | ||
1238 | case EXIT_THREAD_DEBUG_EVENT: | |
1239 | OUTMSG2 (("gdbserver: kernel event EXIT_THREAD_DEBUG_EVENT " | |
1240 | "for pid=%d tid=%x\n", | |
1241 | (unsigned) current_event.dwProcessId, | |
1242 | (unsigned) current_event.dwThreadId)); | |
1243 | child_delete_thread (current_event.dwThreadId); | |
b80864fb DJ |
1244 | break; |
1245 | ||
1246 | case CREATE_PROCESS_DEBUG_EVENT: | |
1247 | OUTMSG2 (("gdbserver: kernel event CREATE_PROCESS_DEBUG_EVENT " | |
1248 | "for pid=%d tid=%x\n", | |
1249 | (unsigned) current_event.dwProcessId, | |
1250 | (unsigned) current_event.dwThreadId)); | |
1251 | CloseHandle (current_event.u.CreateProcessInfo.hFile); | |
1252 | ||
1253 | current_process_handle = current_event.u.CreateProcessInfo.hProcess; | |
1254 | main_thread_id = current_event.dwThreadId; | |
1255 | ||
1256 | ourstatus->kind = TARGET_WAITKIND_EXECD; | |
1257 | ourstatus->value.execd_pathname = "Main executable"; | |
1258 | ||
1259 | /* Add the main thread. */ | |
34b34921 PA |
1260 | child_add_thread (main_thread_id, |
1261 | current_event.u.CreateProcessInfo.hThread); | |
b80864fb | 1262 | |
34b34921 | 1263 | ourstatus->value.related_pid = current_event.dwThreadId; |
ed50f18f PA |
1264 | #ifdef _WIN32_WCE |
1265 | /* Windows CE doesn't set the initial breakpoint automatically | |
1266 | like the desktop versions of Windows do. We add it explicitly | |
1267 | here. It will be removed as soon as it is hit. */ | |
1268 | set_breakpoint_at ((CORE_ADDR) (long) current_event.u | |
1269 | .CreateProcessInfo.lpStartAddress, | |
1270 | delete_breakpoint_at); | |
1271 | #endif | |
b80864fb DJ |
1272 | break; |
1273 | ||
1274 | case EXIT_PROCESS_DEBUG_EVENT: | |
1275 | OUTMSG2 (("gdbserver: kernel event EXIT_PROCESS_DEBUG_EVENT " | |
1276 | "for pid=%d tid=%x\n", | |
1277 | (unsigned) current_event.dwProcessId, | |
1278 | (unsigned) current_event.dwThreadId)); | |
1279 | ourstatus->kind = TARGET_WAITKIND_EXITED; | |
1280 | ourstatus->value.integer = current_event.u.ExitProcess.dwExitCode; | |
1281 | CloseHandle (current_process_handle); | |
9d606399 | 1282 | current_process_handle = NULL; |
b80864fb DJ |
1283 | break; |
1284 | ||
1285 | case LOAD_DLL_DEBUG_EVENT: | |
1286 | OUTMSG2 (("gdbserver: kernel event LOAD_DLL_DEBUG_EVENT " | |
1287 | "for pid=%d tid=%x\n", | |
1288 | (unsigned) current_event.dwProcessId, | |
1289 | (unsigned) current_event.dwThreadId)); | |
1290 | CloseHandle (current_event.u.LoadDll.hFile); | |
255e7678 | 1291 | handle_load_dll (); |
b80864fb DJ |
1292 | |
1293 | ourstatus->kind = TARGET_WAITKIND_LOADED; | |
255e7678 | 1294 | ourstatus->value.sig = TARGET_SIGNAL_TRAP; |
b80864fb DJ |
1295 | break; |
1296 | ||
1297 | case UNLOAD_DLL_DEBUG_EVENT: | |
1298 | OUTMSG2 (("gdbserver: kernel event UNLOAD_DLL_DEBUG_EVENT " | |
1299 | "for pid=%d tid=%x\n", | |
1300 | (unsigned) current_event.dwProcessId, | |
1301 | (unsigned) current_event.dwThreadId)); | |
255e7678 DJ |
1302 | handle_unload_dll (); |
1303 | ourstatus->kind = TARGET_WAITKIND_LOADED; | |
1304 | ourstatus->value.sig = TARGET_SIGNAL_TRAP; | |
b80864fb DJ |
1305 | break; |
1306 | ||
1307 | case EXCEPTION_DEBUG_EVENT: | |
1308 | OUTMSG2 (("gdbserver: kernel event EXCEPTION_DEBUG_EVENT " | |
1309 | "for pid=%d tid=%x\n", | |
1310 | (unsigned) current_event.dwProcessId, | |
1311 | (unsigned) current_event.dwThreadId)); | |
34b34921 | 1312 | handle_exception (ourstatus); |
b80864fb DJ |
1313 | break; |
1314 | ||
1315 | case OUTPUT_DEBUG_STRING_EVENT: | |
1316 | /* A message from the kernel (or Cygwin). */ | |
1317 | OUTMSG2 (("gdbserver: kernel event OUTPUT_DEBUG_STRING_EVENT " | |
1318 | "for pid=%d tid=%x\n", | |
1319 | (unsigned) current_event.dwProcessId, | |
1320 | (unsigned) current_event.dwThreadId)); | |
bce7165d | 1321 | handle_output_debug_string (ourstatus); |
b80864fb DJ |
1322 | break; |
1323 | ||
1324 | default: | |
1325 | OUTMSG2 (("gdbserver: kernel event unknown " | |
1326 | "for pid=%d tid=%x code=%ld\n", | |
1327 | (unsigned) current_event.dwProcessId, | |
1328 | (unsigned) current_event.dwThreadId, | |
1329 | current_event.dwDebugEventCode)); | |
1330 | break; | |
1331 | } | |
1332 | ||
1333 | current_inferior = | |
1334 | (struct thread_info *) find_inferior_id (&all_threads, | |
1335 | current_event.dwThreadId); | |
b80864fb DJ |
1336 | } |
1337 | ||
1338 | /* Wait for the inferior process to change state. | |
1339 | STATUS will be filled in with a response code to send to GDB. | |
1340 | Returns the signal which caused the process to stop. */ | |
1341 | static unsigned char | |
1342 | win32_wait (char *status) | |
1343 | { | |
1344 | struct target_waitstatus our_status; | |
1345 | ||
1346 | *status = 'T'; | |
1347 | ||
1348 | while (1) | |
1349 | { | |
7390519e PA |
1350 | /* Check if GDB sent us an interrupt request. */ |
1351 | check_remote_input_interrupt_request (); | |
1352 | ||
b80864fb DJ |
1353 | get_child_debug_event (&our_status); |
1354 | ||
34b34921 | 1355 | switch (our_status.kind) |
b80864fb | 1356 | { |
34b34921 | 1357 | case TARGET_WAITKIND_EXITED: |
b80864fb DJ |
1358 | OUTMSG2 (("Child exited with retcode = %x\n", |
1359 | our_status.value.integer)); | |
1360 | ||
1361 | *status = 'W'; | |
1362 | ||
1363 | child_fetch_inferior_registers (-1); | |
1364 | ||
1365 | return our_status.value.integer; | |
34b34921 | 1366 | case TARGET_WAITKIND_STOPPED: |
255e7678 | 1367 | case TARGET_WAITKIND_LOADED: |
f72f3e60 | 1368 | OUTMSG2 (("Child Stopped with signal = %d \n", |
ed50f18f | 1369 | our_status.value.sig)); |
b80864fb DJ |
1370 | |
1371 | *status = 'T'; | |
1372 | ||
1373 | child_fetch_inferior_registers (-1); | |
1374 | ||
255e7678 DJ |
1375 | if (our_status.kind == TARGET_WAITKIND_LOADED |
1376 | && !server_waiting) | |
1377 | { | |
1378 | /* When gdb connects, we want to be stopped at the | |
1379 | initial breakpoint, not in some dll load event. */ | |
1380 | child_continue (DBG_CONTINUE, -1); | |
1381 | break; | |
1382 | } | |
1383 | ||
b80864fb | 1384 | return our_status.value.sig; |
34b34921 PA |
1385 | default: |
1386 | OUTMSG (("Ignoring unknown internal event, %d\n", our_status.kind)); | |
1387 | /* fall-through */ | |
1388 | case TARGET_WAITKIND_SPURIOUS: | |
34b34921 PA |
1389 | case TARGET_WAITKIND_EXECD: |
1390 | /* do nothing, just continue */ | |
1391 | child_continue (DBG_CONTINUE, -1); | |
1392 | break; | |
b80864fb | 1393 | } |
b80864fb DJ |
1394 | } |
1395 | } | |
1396 | ||
1397 | /* Fetch registers from the inferior process. | |
1398 | If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */ | |
1399 | static void | |
1400 | win32_fetch_inferior_registers (int regno) | |
1401 | { | |
1402 | child_fetch_inferior_registers (regno); | |
1403 | } | |
1404 | ||
1405 | /* Store registers to the inferior process. | |
1406 | If REGNO is -1, store all registers; otherwise, store at least REGNO. */ | |
1407 | static void | |
1408 | win32_store_inferior_registers (int regno) | |
1409 | { | |
1410 | child_store_inferior_registers (regno); | |
1411 | } | |
1412 | ||
1413 | /* Read memory from the inferior process. This should generally be | |
1414 | called through read_inferior_memory, which handles breakpoint shadowing. | |
1415 | Read LEN bytes at MEMADDR into a buffer at MYADDR. */ | |
1416 | static int | |
1417 | win32_read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len) | |
1418 | { | |
ed50f18f | 1419 | return child_xfer_memory (memaddr, (char *) myaddr, len, 0, 0) != len; |
b80864fb DJ |
1420 | } |
1421 | ||
1422 | /* Write memory to the inferior process. This should generally be | |
1423 | called through write_inferior_memory, which handles breakpoint shadowing. | |
1424 | Write LEN bytes from the buffer at MYADDR to MEMADDR. | |
1425 | Returns 0 on success and errno on failure. */ | |
1426 | static int | |
1427 | win32_write_inferior_memory (CORE_ADDR memaddr, const unsigned char *myaddr, | |
1428 | int len) | |
1429 | { | |
1430 | return child_xfer_memory (memaddr, (char *) myaddr, len, 1, 0) != len; | |
1431 | } | |
1432 | ||
7390519e PA |
1433 | /* Send an interrupt request to the inferior process. */ |
1434 | static void | |
1435 | win32_request_interrupt (void) | |
1436 | { | |
1437 | winapi_DebugBreakProcess DebugBreakProcess; | |
1438 | winapi_GenerateConsoleCtrlEvent GenerateConsoleCtrlEvent; | |
1439 | ||
1440 | #ifdef _WIN32_WCE | |
1441 | HMODULE dll = GetModuleHandle (_T("COREDLL.DLL")); | |
1442 | #else | |
1443 | HMODULE dll = GetModuleHandle (_T("KERNEL32.DLL")); | |
1444 | #endif | |
1445 | ||
1446 | GenerateConsoleCtrlEvent = GETPROCADDRESS (dll, GenerateConsoleCtrlEvent); | |
1447 | ||
1448 | if (GenerateConsoleCtrlEvent != NULL | |
1449 | && GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT, current_process_id)) | |
1450 | return; | |
1451 | ||
1452 | /* GenerateConsoleCtrlEvent can fail if process id being debugged is | |
1453 | not a process group id. | |
1454 | Fallback to XP/Vista 'DebugBreakProcess', which generates a | |
1455 | breakpoint exception in the interior process. */ | |
1456 | ||
1457 | DebugBreakProcess = GETPROCADDRESS (dll, DebugBreakProcess); | |
1458 | ||
1459 | if (DebugBreakProcess != NULL | |
1460 | && DebugBreakProcess (current_process_handle)) | |
1461 | return; | |
1462 | ||
1463 | OUTMSG (("Could not interrupt process.\n")); | |
1464 | } | |
1465 | ||
820f2bda PA |
1466 | static const char * |
1467 | win32_arch_string (void) | |
1468 | { | |
ed50f18f | 1469 | return the_low_target.arch_string; |
820f2bda PA |
1470 | } |
1471 | ||
b80864fb DJ |
1472 | static struct target_ops win32_target_ops = { |
1473 | win32_create_inferior, | |
1474 | win32_attach, | |
1475 | win32_kill, | |
1476 | win32_detach, | |
444d6139 | 1477 | win32_join, |
b80864fb DJ |
1478 | win32_thread_alive, |
1479 | win32_resume, | |
1480 | win32_wait, | |
1481 | win32_fetch_inferior_registers, | |
1482 | win32_store_inferior_registers, | |
1483 | win32_read_inferior_memory, | |
1484 | win32_write_inferior_memory, | |
820f2bda | 1485 | NULL, |
7390519e | 1486 | win32_request_interrupt, |
820f2bda PA |
1487 | NULL, |
1488 | NULL, | |
1489 | NULL, | |
1490 | NULL, | |
1491 | NULL, | |
1492 | NULL, | |
1493 | NULL, | |
1494 | win32_arch_string | |
b80864fb DJ |
1495 | }; |
1496 | ||
1497 | /* Initialize the Win32 backend. */ | |
1498 | void | |
1499 | initialize_low (void) | |
1500 | { | |
1501 | set_target_ops (&win32_target_ops); | |
ed50f18f PA |
1502 | if (the_low_target.breakpoint != NULL) |
1503 | set_breakpoint_data (the_low_target.breakpoint, | |
1504 | the_low_target.breakpoint_len); | |
b80864fb DJ |
1505 | init_registers (); |
1506 | } |