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