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