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