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