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