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