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