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