win32-low.cc revision 1.1.1.2 1 1.1 christos /* Low level interface to Windows debugging, for gdbserver.
2 1.1.1.2 christos Copyright (C) 2006-2023 Free Software Foundation, Inc.
3 1.1 christos
4 1.1 christos Contributed by Leo Zayas. Based on "win32-nat.c" from GDB.
5 1.1 christos
6 1.1 christos This file is part of GDB.
7 1.1 christos
8 1.1 christos This program is free software; you can redistribute it and/or modify
9 1.1 christos it under the terms of the GNU General Public License as published by
10 1.1 christos the Free Software Foundation; either version 3 of the License, or
11 1.1 christos (at your option) any later version.
12 1.1 christos
13 1.1 christos This program is distributed in the hope that it will be useful,
14 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
15 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 1.1 christos GNU General Public License for more details.
17 1.1 christos
18 1.1 christos You should have received a copy of the GNU General Public License
19 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 1.1 christos
21 1.1 christos #include "server.h"
22 1.1 christos #include "regcache.h"
23 1.1.1.2 christos #include "gdbsupport/fileio.h"
24 1.1 christos #include "mem-break.h"
25 1.1 christos #include "win32-low.h"
26 1.1 christos #include "gdbthread.h"
27 1.1 christos #include "dll.h"
28 1.1 christos #include "hostio.h"
29 1.1 christos #include <windows.h>
30 1.1 christos #include <winnt.h>
31 1.1 christos #include <imagehlp.h>
32 1.1 christos #include <tlhelp32.h>
33 1.1 christos #include <psapi.h>
34 1.1 christos #include <process.h>
35 1.1 christos #include "gdbsupport/gdb_tilde_expand.h"
36 1.1 christos #include "gdbsupport/common-inferior.h"
37 1.1 christos #include "gdbsupport/gdb_wait.h"
38 1.1 christos
39 1.1 christos using namespace windows_nat;
40 1.1 christos
41 1.1.1.2 christos /* See win32-low.h. */
42 1.1.1.2 christos gdbserver_windows_process windows_process;
43 1.1.1.2 christos
44 1.1 christos #ifndef USE_WIN32API
45 1.1 christos #include <sys/cygwin.h>
46 1.1 christos #endif
47 1.1 christos
48 1.1 christos #define OUTMSG(X) do { printf X; fflush (stderr); } while (0)
49 1.1 christos
50 1.1 christos #define OUTMSG2(X) \
51 1.1 christos do \
52 1.1 christos { \
53 1.1 christos if (debug_threads) \
54 1.1 christos { \
55 1.1 christos printf X; \
56 1.1 christos fflush (stderr); \
57 1.1 christos } \
58 1.1 christos } while (0)
59 1.1 christos
60 1.1 christos #ifndef _T
61 1.1 christos #define _T(x) TEXT (x)
62 1.1 christos #endif
63 1.1 christos
64 1.1 christos int using_threads = 1;
65 1.1 christos
66 1.1 christos const struct target_desc *win32_tdesc;
67 1.1 christos #ifdef __x86_64__
68 1.1 christos const struct target_desc *wow64_win32_tdesc;
69 1.1 christos #endif
70 1.1 christos
71 1.1 christos #define NUM_REGS (the_low_target.num_regs ())
72 1.1 christos
73 1.1 christos /* Get the thread ID from the current selected inferior (the current
74 1.1 christos thread). */
75 1.1 christos static ptid_t
76 1.1 christos current_thread_ptid (void)
77 1.1 christos {
78 1.1 christos return current_ptid;
79 1.1 christos }
80 1.1 christos
81 1.1 christos /* The current debug event from WaitForDebugEvent. */
82 1.1 christos static ptid_t
83 1.1 christos debug_event_ptid (DEBUG_EVENT *event)
84 1.1 christos {
85 1.1 christos return ptid_t (event->dwProcessId, event->dwThreadId, 0);
86 1.1 christos }
87 1.1 christos
88 1.1 christos /* Get the thread context of the thread associated with TH. */
89 1.1 christos
90 1.1 christos static void
91 1.1 christos win32_get_thread_context (windows_thread_info *th)
92 1.1 christos {
93 1.1 christos #ifdef __x86_64__
94 1.1.1.2 christos if (windows_process.wow64_process)
95 1.1 christos memset (&th->wow64_context, 0, sizeof (WOW64_CONTEXT));
96 1.1 christos else
97 1.1 christos #endif
98 1.1 christos memset (&th->context, 0, sizeof (CONTEXT));
99 1.1 christos (*the_low_target.get_thread_context) (th);
100 1.1 christos }
101 1.1 christos
102 1.1 christos /* Set the thread context of the thread associated with TH. */
103 1.1 christos
104 1.1 christos static void
105 1.1 christos win32_set_thread_context (windows_thread_info *th)
106 1.1 christos {
107 1.1 christos #ifdef __x86_64__
108 1.1.1.2 christos if (windows_process.wow64_process)
109 1.1.1.2 christos Wow64SetThreadContext (th->h, &th->wow64_context);
110 1.1.1.2 christos else
111 1.1 christos #endif
112 1.1.1.2 christos SetThreadContext (th->h, &th->context);
113 1.1 christos }
114 1.1 christos
115 1.1 christos /* Set the thread context of the thread associated with TH. */
116 1.1 christos
117 1.1 christos static void
118 1.1 christos win32_prepare_to_resume (windows_thread_info *th)
119 1.1 christos {
120 1.1 christos if (the_low_target.prepare_to_resume != NULL)
121 1.1 christos (*the_low_target.prepare_to_resume) (th);
122 1.1 christos }
123 1.1 christos
124 1.1 christos /* See win32-low.h. */
125 1.1 christos
126 1.1 christos void
127 1.1 christos win32_require_context (windows_thread_info *th)
128 1.1 christos {
129 1.1 christos DWORD context_flags;
130 1.1 christos #ifdef __x86_64__
131 1.1.1.2 christos if (windows_process.wow64_process)
132 1.1 christos context_flags = th->wow64_context.ContextFlags;
133 1.1 christos else
134 1.1 christos #endif
135 1.1 christos context_flags = th->context.ContextFlags;
136 1.1 christos if (context_flags == 0)
137 1.1 christos {
138 1.1 christos th->suspend ();
139 1.1 christos win32_get_thread_context (th);
140 1.1 christos }
141 1.1 christos }
142 1.1 christos
143 1.1 christos /* See nat/windows-nat.h. */
144 1.1 christos
145 1.1 christos windows_thread_info *
146 1.1.1.2 christos gdbserver_windows_process::thread_rec
147 1.1.1.2 christos (ptid_t ptid, thread_disposition_type disposition)
148 1.1 christos {
149 1.1 christos thread_info *thread = find_thread_ptid (ptid);
150 1.1 christos if (thread == NULL)
151 1.1 christos return NULL;
152 1.1 christos
153 1.1 christos windows_thread_info *th = (windows_thread_info *) thread_target_data (thread);
154 1.1 christos if (disposition != DONT_INVALIDATE_CONTEXT)
155 1.1 christos win32_require_context (th);
156 1.1 christos return th;
157 1.1 christos }
158 1.1 christos
159 1.1 christos /* Add a thread to the thread list. */
160 1.1 christos static windows_thread_info *
161 1.1 christos child_add_thread (DWORD pid, DWORD tid, HANDLE h, void *tlb)
162 1.1 christos {
163 1.1 christos windows_thread_info *th;
164 1.1 christos ptid_t ptid = ptid_t (pid, tid, 0);
165 1.1 christos
166 1.1.1.2 christos if ((th = windows_process.thread_rec (ptid, DONT_INVALIDATE_CONTEXT)))
167 1.1 christos return th;
168 1.1 christos
169 1.1 christos CORE_ADDR base = (CORE_ADDR) (uintptr_t) tlb;
170 1.1 christos #ifdef __x86_64__
171 1.1 christos /* For WOW64 processes, this is actually the pointer to the 64bit TIB,
172 1.1 christos and the 32bit TIB is exactly 2 pages after it. */
173 1.1.1.2 christos if (windows_process.wow64_process)
174 1.1 christos base += 2 * 4096; /* page size = 4096 */
175 1.1 christos #endif
176 1.1 christos th = new windows_thread_info (tid, h, base);
177 1.1 christos
178 1.1 christos add_thread (ptid, th);
179 1.1 christos
180 1.1 christos if (the_low_target.thread_added != NULL)
181 1.1 christos (*the_low_target.thread_added) (th);
182 1.1 christos
183 1.1 christos return th;
184 1.1 christos }
185 1.1 christos
186 1.1 christos /* Delete a thread from the list of threads. */
187 1.1 christos static void
188 1.1 christos delete_thread_info (thread_info *thread)
189 1.1 christos {
190 1.1 christos windows_thread_info *th = (windows_thread_info *) thread_target_data (thread);
191 1.1 christos
192 1.1 christos remove_thread (thread);
193 1.1 christos delete th;
194 1.1 christos }
195 1.1 christos
196 1.1 christos /* Delete a thread from the list of threads. */
197 1.1 christos static void
198 1.1 christos child_delete_thread (DWORD pid, DWORD tid)
199 1.1 christos {
200 1.1 christos /* If the last thread is exiting, just return. */
201 1.1 christos if (all_threads.size () == 1)
202 1.1 christos return;
203 1.1 christos
204 1.1 christos thread_info *thread = find_thread_ptid (ptid_t (pid, tid));
205 1.1 christos if (thread == NULL)
206 1.1 christos return;
207 1.1 christos
208 1.1 christos delete_thread_info (thread);
209 1.1 christos }
210 1.1 christos
211 1.1 christos /* These watchpoint related wrapper functions simply pass on the function call
212 1.1 christos if the low target has registered a corresponding function. */
213 1.1 christos
214 1.1 christos bool
215 1.1 christos win32_process_target::supports_z_point_type (char z_type)
216 1.1 christos {
217 1.1 christos return (z_type == Z_PACKET_SW_BP
218 1.1 christos || (the_low_target.supports_z_point_type != NULL
219 1.1 christos && the_low_target.supports_z_point_type (z_type)));
220 1.1 christos }
221 1.1 christos
222 1.1 christos int
223 1.1 christos win32_process_target::insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
224 1.1 christos int size, raw_breakpoint *bp)
225 1.1 christos {
226 1.1 christos if (type == raw_bkpt_type_sw)
227 1.1 christos return insert_memory_breakpoint (bp);
228 1.1 christos else if (the_low_target.insert_point != NULL)
229 1.1 christos return the_low_target.insert_point (type, addr, size, bp);
230 1.1 christos else
231 1.1 christos /* Unsupported (see target.h). */
232 1.1 christos return 1;
233 1.1 christos }
234 1.1 christos
235 1.1 christos int
236 1.1 christos win32_process_target::remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
237 1.1 christos int size, raw_breakpoint *bp)
238 1.1 christos {
239 1.1 christos if (type == raw_bkpt_type_sw)
240 1.1 christos return remove_memory_breakpoint (bp);
241 1.1 christos else if (the_low_target.remove_point != NULL)
242 1.1 christos return the_low_target.remove_point (type, addr, size, bp);
243 1.1 christos else
244 1.1 christos /* Unsupported (see target.h). */
245 1.1 christos return 1;
246 1.1 christos }
247 1.1 christos
248 1.1 christos bool
249 1.1 christos win32_process_target::stopped_by_watchpoint ()
250 1.1 christos {
251 1.1 christos if (the_low_target.stopped_by_watchpoint != NULL)
252 1.1 christos return the_low_target.stopped_by_watchpoint ();
253 1.1 christos else
254 1.1 christos return false;
255 1.1 christos }
256 1.1 christos
257 1.1 christos CORE_ADDR
258 1.1 christos win32_process_target::stopped_data_address ()
259 1.1 christos {
260 1.1 christos if (the_low_target.stopped_data_address != NULL)
261 1.1 christos return the_low_target.stopped_data_address ();
262 1.1 christos else
263 1.1 christos return 0;
264 1.1 christos }
265 1.1 christos
266 1.1 christos
267 1.1 christos /* Transfer memory from/to the debugged process. */
268 1.1 christos static int
269 1.1 christos child_xfer_memory (CORE_ADDR memaddr, char *our, int len,
270 1.1 christos int write, process_stratum_target *target)
271 1.1 christos {
272 1.1 christos BOOL success;
273 1.1 christos SIZE_T done = 0;
274 1.1 christos DWORD lasterror = 0;
275 1.1 christos uintptr_t addr = (uintptr_t) memaddr;
276 1.1 christos
277 1.1 christos if (write)
278 1.1 christos {
279 1.1.1.2 christos success = WriteProcessMemory (windows_process.handle, (LPVOID) addr,
280 1.1 christos (LPCVOID) our, len, &done);
281 1.1 christos if (!success)
282 1.1 christos lasterror = GetLastError ();
283 1.1.1.2 christos FlushInstructionCache (windows_process.handle, (LPCVOID) addr, len);
284 1.1 christos }
285 1.1 christos else
286 1.1 christos {
287 1.1.1.2 christos success = ReadProcessMemory (windows_process.handle, (LPCVOID) addr,
288 1.1 christos (LPVOID) our, len, &done);
289 1.1 christos if (!success)
290 1.1 christos lasterror = GetLastError ();
291 1.1 christos }
292 1.1 christos if (!success && lasterror == ERROR_PARTIAL_COPY && done > 0)
293 1.1 christos return done;
294 1.1 christos else
295 1.1 christos return success ? done : -1;
296 1.1 christos }
297 1.1 christos
298 1.1 christos /* Clear out any old thread list and reinitialize it to a pristine
299 1.1 christos state. */
300 1.1 christos static void
301 1.1 christos child_init_thread_list (void)
302 1.1 christos {
303 1.1 christos for_each_thread (delete_thread_info);
304 1.1 christos }
305 1.1 christos
306 1.1 christos static void
307 1.1 christos do_initial_child_stuff (HANDLE proch, DWORD pid, int attached)
308 1.1 christos {
309 1.1 christos struct process_info *proc;
310 1.1 christos
311 1.1.1.2 christos windows_process.last_sig = GDB_SIGNAL_0;
312 1.1.1.2 christos windows_process.handle = proch;
313 1.1.1.2 christos windows_process.main_thread_id = 0;
314 1.1.1.2 christos
315 1.1.1.2 christos windows_process.soft_interrupt_requested = 0;
316 1.1.1.2 christos windows_process.faked_breakpoint = 0;
317 1.1.1.2 christos windows_process.open_process_used = true;
318 1.1 christos
319 1.1.1.2 christos memset (&windows_process.current_event, 0,
320 1.1.1.2 christos sizeof (windows_process.current_event));
321 1.1 christos
322 1.1 christos #ifdef __x86_64__
323 1.1 christos BOOL wow64;
324 1.1 christos if (!IsWow64Process (proch, &wow64))
325 1.1 christos {
326 1.1 christos DWORD err = GetLastError ();
327 1.1 christos error ("Check if WOW64 process failed (error %d): %s\n",
328 1.1 christos (int) err, strwinerror (err));
329 1.1 christos }
330 1.1.1.2 christos windows_process.wow64_process = wow64;
331 1.1 christos
332 1.1.1.2 christos if (windows_process.wow64_process
333 1.1.1.2 christos && (Wow64GetThreadContext == nullptr
334 1.1.1.2 christos || Wow64SetThreadContext == nullptr))
335 1.1 christos error ("WOW64 debugging is not supported on this system.\n");
336 1.1 christos
337 1.1.1.2 christos windows_process.ignore_first_breakpoint
338 1.1.1.2 christos = !attached && windows_process.wow64_process;
339 1.1 christos #endif
340 1.1 christos
341 1.1 christos proc = add_process (pid, attached);
342 1.1 christos #ifdef __x86_64__
343 1.1.1.2 christos if (windows_process.wow64_process)
344 1.1 christos proc->tdesc = wow64_win32_tdesc;
345 1.1 christos else
346 1.1 christos #endif
347 1.1 christos proc->tdesc = win32_tdesc;
348 1.1 christos child_init_thread_list ();
349 1.1.1.2 christos windows_process.child_initialization_done = 0;
350 1.1 christos
351 1.1 christos if (the_low_target.initial_stuff != NULL)
352 1.1 christos (*the_low_target.initial_stuff) ();
353 1.1 christos
354 1.1.1.2 christos windows_process.cached_status.set_ignore ();
355 1.1 christos
356 1.1 christos /* Flush all currently pending debug events (thread and dll list) up
357 1.1 christos to the initial breakpoint. */
358 1.1 christos while (1)
359 1.1 christos {
360 1.1 christos struct target_waitstatus status;
361 1.1 christos
362 1.1 christos the_target->wait (minus_one_ptid, &status, 0);
363 1.1 christos
364 1.1 christos /* Note win32_wait doesn't return thread events. */
365 1.1.1.2 christos if (status.kind () != TARGET_WAITKIND_LOADED)
366 1.1 christos {
367 1.1.1.2 christos windows_process.cached_status = status;
368 1.1 christos break;
369 1.1 christos }
370 1.1 christos
371 1.1 christos {
372 1.1 christos struct thread_resume resume;
373 1.1 christos
374 1.1 christos resume.thread = minus_one_ptid;
375 1.1 christos resume.kind = resume_continue;
376 1.1 christos resume.sig = 0;
377 1.1 christos
378 1.1 christos the_target->resume (&resume, 1);
379 1.1 christos }
380 1.1 christos }
381 1.1 christos
382 1.1 christos /* Now that the inferior has been started and all DLLs have been mapped,
383 1.1 christos we can iterate over all DLLs and load them in.
384 1.1 christos
385 1.1 christos We avoid doing it any earlier because, on certain versions of Windows,
386 1.1 christos LOAD_DLL_DEBUG_EVENTs are sometimes not complete. In particular,
387 1.1 christos we have seen on Windows 8.1 that the ntdll.dll load event does not
388 1.1 christos include the DLL name, preventing us from creating an associated SO.
389 1.1 christos A possible explanation is that ntdll.dll might be mapped before
390 1.1 christos the SO info gets created by the Windows system -- ntdll.dll is
391 1.1 christos the first DLL to be reported via LOAD_DLL_DEBUG_EVENT and other DLLs
392 1.1 christos do not seem to suffer from that problem.
393 1.1 christos
394 1.1 christos Rather than try to work around this sort of issue, it is much
395 1.1 christos simpler to just ignore DLL load/unload events during the startup
396 1.1 christos phase, and then process them all in one batch now. */
397 1.1.1.2 christos windows_process.add_all_dlls ();
398 1.1 christos
399 1.1.1.2 christos windows_process.child_initialization_done = 1;
400 1.1 christos }
401 1.1 christos
402 1.1 christos /* Resume all artificially suspended threads if we are continuing
403 1.1 christos execution. */
404 1.1 christos static void
405 1.1 christos continue_one_thread (thread_info *thread, int thread_id)
406 1.1 christos {
407 1.1 christos windows_thread_info *th = (windows_thread_info *) thread_target_data (thread);
408 1.1 christos
409 1.1 christos if (thread_id == -1 || thread_id == th->tid)
410 1.1 christos {
411 1.1 christos win32_prepare_to_resume (th);
412 1.1 christos
413 1.1 christos if (th->suspended)
414 1.1 christos {
415 1.1 christos DWORD *context_flags;
416 1.1 christos #ifdef __x86_64__
417 1.1.1.2 christos if (windows_process.wow64_process)
418 1.1 christos context_flags = &th->wow64_context.ContextFlags;
419 1.1 christos else
420 1.1 christos #endif
421 1.1 christos context_flags = &th->context.ContextFlags;
422 1.1 christos if (*context_flags)
423 1.1 christos {
424 1.1 christos win32_set_thread_context (th);
425 1.1 christos *context_flags = 0;
426 1.1 christos }
427 1.1 christos
428 1.1 christos th->resume ();
429 1.1 christos }
430 1.1 christos }
431 1.1 christos }
432 1.1 christos
433 1.1 christos static BOOL
434 1.1 christos child_continue (DWORD continue_status, int thread_id)
435 1.1 christos {
436 1.1.1.2 christos windows_process.desired_stop_thread_id = thread_id;
437 1.1.1.2 christos if (windows_process.matching_pending_stop (debug_threads))
438 1.1 christos return TRUE;
439 1.1 christos
440 1.1 christos /* The inferior will only continue after the ContinueDebugEvent
441 1.1 christos call. */
442 1.1 christos for_each_thread ([&] (thread_info *thread)
443 1.1 christos {
444 1.1 christos continue_one_thread (thread, thread_id);
445 1.1 christos });
446 1.1.1.2 christos windows_process.faked_breakpoint = 0;
447 1.1 christos
448 1.1 christos return continue_last_debug_event (continue_status, debug_threads);
449 1.1 christos }
450 1.1 christos
451 1.1 christos /* Fetch register(s) from the current thread context. */
452 1.1 christos static void
453 1.1 christos child_fetch_inferior_registers (struct regcache *regcache, int r)
454 1.1 christos {
455 1.1 christos int regno;
456 1.1.1.2 christos windows_thread_info *th
457 1.1.1.2 christos = windows_process.thread_rec (current_thread_ptid (),
458 1.1.1.2 christos INVALIDATE_CONTEXT);
459 1.1 christos if (r == -1 || r > NUM_REGS)
460 1.1 christos child_fetch_inferior_registers (regcache, NUM_REGS);
461 1.1 christos else
462 1.1 christos for (regno = 0; regno < r; regno++)
463 1.1 christos (*the_low_target.fetch_inferior_register) (regcache, th, regno);
464 1.1 christos }
465 1.1 christos
466 1.1 christos /* Store a new register value into the current thread context. We don't
467 1.1 christos change the program's context until later, when we resume it. */
468 1.1 christos static void
469 1.1 christos child_store_inferior_registers (struct regcache *regcache, int r)
470 1.1 christos {
471 1.1 christos int regno;
472 1.1.1.2 christos windows_thread_info *th
473 1.1.1.2 christos = windows_process.thread_rec (current_thread_ptid (),
474 1.1.1.2 christos INVALIDATE_CONTEXT);
475 1.1 christos if (r == -1 || r == 0 || r > NUM_REGS)
476 1.1 christos child_store_inferior_registers (regcache, NUM_REGS);
477 1.1 christos else
478 1.1 christos for (regno = 0; regno < r; regno++)
479 1.1 christos (*the_low_target.store_inferior_register) (regcache, th, regno);
480 1.1 christos }
481 1.1 christos
482 1.1 christos static BOOL
483 1.1 christos create_process (const char *program, char *args,
484 1.1 christos DWORD flags, PROCESS_INFORMATION *pi)
485 1.1 christos {
486 1.1.1.2 christos const std::string &inferior_cwd = get_inferior_cwd ();
487 1.1 christos BOOL ret;
488 1.1 christos size_t argslen, proglen;
489 1.1 christos
490 1.1 christos proglen = strlen (program) + 1;
491 1.1 christos argslen = strlen (args) + proglen;
492 1.1 christos
493 1.1 christos STARTUPINFOA si = { sizeof (STARTUPINFOA) };
494 1.1 christos char *program_and_args = (char *) alloca (argslen + 1);
495 1.1 christos
496 1.1 christos strcpy (program_and_args, program);
497 1.1 christos strcat (program_and_args, " ");
498 1.1 christos strcat (program_and_args, args);
499 1.1.1.2 christos ret = create_process (program, /* image name */
500 1.1 christos program_and_args, /* command line */
501 1.1 christos flags, /* start flags */
502 1.1 christos NULL, /* environment */
503 1.1 christos /* current directory */
504 1.1.1.2 christos (inferior_cwd.empty ()
505 1.1 christos ? NULL
506 1.1.1.2 christos : gdb_tilde_expand (inferior_cwd.c_str ()).c_str()),
507 1.1.1.2 christos get_client_state ().disable_randomization,
508 1.1 christos &si, /* start info */
509 1.1 christos pi); /* proc info */
510 1.1 christos
511 1.1 christos return ret;
512 1.1 christos }
513 1.1 christos
514 1.1 christos /* Start a new process.
515 1.1 christos PROGRAM is the program name.
516 1.1 christos PROGRAM_ARGS is the vector containing the inferior's args.
517 1.1 christos Returns the new PID on success, -1 on failure. Registers the new
518 1.1 christos process with the process list. */
519 1.1 christos int
520 1.1 christos win32_process_target::create_inferior (const char *program,
521 1.1 christos const std::vector<char *> &program_args)
522 1.1 christos {
523 1.1 christos client_state &cs = get_client_state ();
524 1.1 christos #ifndef USE_WIN32API
525 1.1 christos char real_path[PATH_MAX];
526 1.1 christos char *orig_path, *new_path, *path_ptr;
527 1.1 christos #endif
528 1.1 christos BOOL ret;
529 1.1 christos DWORD flags;
530 1.1 christos PROCESS_INFORMATION pi;
531 1.1 christos DWORD err;
532 1.1 christos std::string str_program_args = construct_inferior_arguments (program_args);
533 1.1 christos char *args = (char *) str_program_args.c_str ();
534 1.1 christos
535 1.1 christos /* win32_wait needs to know we're not attaching. */
536 1.1.1.2 christos windows_process.attaching = 0;
537 1.1 christos
538 1.1 christos if (!program)
539 1.1 christos error ("No executable specified, specify executable to debug.\n");
540 1.1 christos
541 1.1 christos flags = DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS;
542 1.1 christos
543 1.1 christos #ifndef USE_WIN32API
544 1.1 christos orig_path = NULL;
545 1.1 christos path_ptr = getenv ("PATH");
546 1.1 christos if (path_ptr)
547 1.1 christos {
548 1.1 christos int size = cygwin_conv_path_list (CCP_POSIX_TO_WIN_A, path_ptr, NULL, 0);
549 1.1 christos orig_path = (char *) alloca (strlen (path_ptr) + 1);
550 1.1 christos new_path = (char *) alloca (size);
551 1.1 christos strcpy (orig_path, path_ptr);
552 1.1 christos cygwin_conv_path_list (CCP_POSIX_TO_WIN_A, path_ptr, new_path, size);
553 1.1 christos setenv ("PATH", new_path, 1);
554 1.1 christos }
555 1.1 christos cygwin_conv_path (CCP_POSIX_TO_WIN_A, program, real_path, PATH_MAX);
556 1.1 christos program = real_path;
557 1.1 christos #endif
558 1.1 christos
559 1.1 christos OUTMSG2 (("Command line is \"%s %s\"\n", program, args));
560 1.1 christos
561 1.1 christos #ifdef CREATE_NEW_PROCESS_GROUP
562 1.1 christos flags |= CREATE_NEW_PROCESS_GROUP;
563 1.1 christos #endif
564 1.1 christos
565 1.1 christos ret = create_process (program, args, flags, &pi);
566 1.1 christos err = GetLastError ();
567 1.1 christos if (!ret && err == ERROR_FILE_NOT_FOUND)
568 1.1 christos {
569 1.1 christos char *exename = (char *) alloca (strlen (program) + 5);
570 1.1 christos strcat (strcpy (exename, program), ".exe");
571 1.1 christos ret = create_process (exename, args, flags, &pi);
572 1.1 christos err = GetLastError ();
573 1.1 christos }
574 1.1 christos
575 1.1 christos #ifndef USE_WIN32API
576 1.1 christos if (orig_path)
577 1.1 christos setenv ("PATH", orig_path, 1);
578 1.1 christos #endif
579 1.1 christos
580 1.1 christos if (!ret)
581 1.1 christos {
582 1.1 christos error ("Error creating process \"%s %s\", (error %d): %s\n",
583 1.1 christos program, args, (int) err, strwinerror (err));
584 1.1 christos }
585 1.1 christos else
586 1.1 christos {
587 1.1 christos OUTMSG2 (("Process created: %s %s\n", program, (char *) args));
588 1.1 christos }
589 1.1 christos
590 1.1 christos CloseHandle (pi.hThread);
591 1.1 christos
592 1.1 christos do_initial_child_stuff (pi.hProcess, pi.dwProcessId, 0);
593 1.1 christos
594 1.1 christos /* Wait till we are at 1st instruction in program, return new pid
595 1.1 christos (assuming success). */
596 1.1.1.2 christos cs.last_ptid = wait (ptid_t (pi.dwProcessId), &cs.last_status, 0);
597 1.1 christos
598 1.1 christos /* Necessary for handle_v_kill. */
599 1.1.1.2 christos signal_pid = pi.dwProcessId;
600 1.1 christos
601 1.1.1.2 christos return pi.dwProcessId;
602 1.1 christos }
603 1.1 christos
604 1.1 christos /* Attach to a running process.
605 1.1 christos PID is the process ID to attach to, specified by the user
606 1.1 christos or a higher layer. */
607 1.1 christos int
608 1.1 christos win32_process_target::attach (unsigned long pid)
609 1.1 christos {
610 1.1 christos HANDLE h;
611 1.1 christos DWORD err;
612 1.1 christos
613 1.1 christos h = OpenProcess (PROCESS_ALL_ACCESS, FALSE, pid);
614 1.1 christos if (h != NULL)
615 1.1 christos {
616 1.1 christos if (DebugActiveProcess (pid))
617 1.1 christos {
618 1.1.1.2 christos DebugSetProcessKillOnExit (FALSE);
619 1.1 christos
620 1.1 christos /* win32_wait needs to know we're attaching. */
621 1.1.1.2 christos windows_process.attaching = 1;
622 1.1 christos do_initial_child_stuff (h, pid, 1);
623 1.1 christos return 0;
624 1.1 christos }
625 1.1 christos
626 1.1 christos CloseHandle (h);
627 1.1 christos }
628 1.1 christos
629 1.1 christos err = GetLastError ();
630 1.1 christos error ("Attach to process failed (error %d): %s\n",
631 1.1 christos (int) err, strwinerror (err));
632 1.1 christos }
633 1.1 christos
634 1.1 christos /* See nat/windows-nat.h. */
635 1.1 christos
636 1.1 christos int
637 1.1.1.2 christos gdbserver_windows_process::handle_output_debug_string
638 1.1.1.2 christos (struct target_waitstatus *ourstatus)
639 1.1 christos {
640 1.1 christos #define READ_BUFFER_LEN 1024
641 1.1 christos CORE_ADDR addr;
642 1.1 christos char s[READ_BUFFER_LEN + 1] = { 0 };
643 1.1 christos DWORD nbytes = current_event.u.DebugString.nDebugStringLength;
644 1.1 christos
645 1.1 christos if (nbytes == 0)
646 1.1 christos return 0;
647 1.1 christos
648 1.1 christos if (nbytes > READ_BUFFER_LEN)
649 1.1 christos nbytes = READ_BUFFER_LEN;
650 1.1 christos
651 1.1 christos addr = (CORE_ADDR) (size_t) current_event.u.DebugString.lpDebugStringData;
652 1.1 christos
653 1.1 christos if (current_event.u.DebugString.fUnicode)
654 1.1 christos {
655 1.1 christos /* The event tells us how many bytes, not chars, even
656 1.1 christos in Unicode. */
657 1.1 christos WCHAR buffer[(READ_BUFFER_LEN + 1) / sizeof (WCHAR)] = { 0 };
658 1.1 christos if (read_inferior_memory (addr, (unsigned char *) buffer, nbytes) != 0)
659 1.1 christos return 0;
660 1.1 christos wcstombs (s, buffer, (nbytes + 1) / sizeof (WCHAR));
661 1.1 christos }
662 1.1 christos else
663 1.1 christos {
664 1.1 christos if (read_inferior_memory (addr, (unsigned char *) s, nbytes) != 0)
665 1.1 christos return 0;
666 1.1 christos }
667 1.1 christos
668 1.1 christos if (!startswith (s, "cYg"))
669 1.1 christos {
670 1.1 christos if (!server_waiting)
671 1.1 christos {
672 1.1 christos OUTMSG2(("%s", s));
673 1.1 christos return 0;
674 1.1 christos }
675 1.1 christos
676 1.1 christos monitor_output (s);
677 1.1 christos }
678 1.1 christos #undef READ_BUFFER_LEN
679 1.1 christos
680 1.1 christos return 0;
681 1.1 christos }
682 1.1 christos
683 1.1 christos static void
684 1.1 christos win32_clear_inferiors (void)
685 1.1 christos {
686 1.1.1.2 christos if (windows_process.open_process_used)
687 1.1 christos {
688 1.1.1.2 christos CloseHandle (windows_process.handle);
689 1.1.1.2 christos windows_process.open_process_used = false;
690 1.1 christos }
691 1.1 christos
692 1.1 christos for_each_thread (delete_thread_info);
693 1.1.1.2 christos windows_process.siginfo_er.ExceptionCode = 0;
694 1.1 christos clear_inferiors ();
695 1.1 christos }
696 1.1 christos
697 1.1 christos /* Implementation of target_ops::kill. */
698 1.1 christos
699 1.1 christos int
700 1.1 christos win32_process_target::kill (process_info *process)
701 1.1 christos {
702 1.1.1.2 christos TerminateProcess (windows_process.handle, 0);
703 1.1 christos for (;;)
704 1.1 christos {
705 1.1 christos if (!child_continue (DBG_CONTINUE, -1))
706 1.1 christos break;
707 1.1.1.2 christos if (!wait_for_debug_event (&windows_process.current_event, INFINITE))
708 1.1 christos break;
709 1.1.1.2 christos if (windows_process.current_event.dwDebugEventCode
710 1.1.1.2 christos == EXIT_PROCESS_DEBUG_EVENT)
711 1.1 christos break;
712 1.1.1.2 christos else if (windows_process.current_event.dwDebugEventCode
713 1.1.1.2 christos == OUTPUT_DEBUG_STRING_EVENT)
714 1.1.1.2 christos windows_process.handle_output_debug_string (nullptr);
715 1.1 christos }
716 1.1 christos
717 1.1 christos win32_clear_inferiors ();
718 1.1 christos
719 1.1 christos remove_process (process);
720 1.1 christos return 0;
721 1.1 christos }
722 1.1 christos
723 1.1 christos /* Implementation of target_ops::detach. */
724 1.1 christos
725 1.1 christos int
726 1.1 christos win32_process_target::detach (process_info *process)
727 1.1 christos {
728 1.1.1.2 christos struct thread_resume resume;
729 1.1.1.2 christos resume.thread = minus_one_ptid;
730 1.1.1.2 christos resume.kind = resume_continue;
731 1.1.1.2 christos resume.sig = 0;
732 1.1.1.2 christos this->resume (&resume, 1);
733 1.1 christos
734 1.1.1.2 christos if (!DebugActiveProcessStop (process->pid))
735 1.1 christos return -1;
736 1.1 christos
737 1.1 christos DebugSetProcessKillOnExit (FALSE);
738 1.1 christos remove_process (process);
739 1.1 christos
740 1.1 christos win32_clear_inferiors ();
741 1.1 christos return 0;
742 1.1 christos }
743 1.1 christos
744 1.1 christos void
745 1.1 christos win32_process_target::mourn (struct process_info *process)
746 1.1 christos {
747 1.1 christos remove_process (process);
748 1.1 christos }
749 1.1 christos
750 1.1 christos /* Implementation of target_ops::join. */
751 1.1 christos
752 1.1 christos void
753 1.1 christos win32_process_target::join (int pid)
754 1.1 christos {
755 1.1 christos HANDLE h = OpenProcess (PROCESS_ALL_ACCESS, FALSE, pid);
756 1.1 christos if (h != NULL)
757 1.1 christos {
758 1.1 christos WaitForSingleObject (h, INFINITE);
759 1.1 christos CloseHandle (h);
760 1.1 christos }
761 1.1 christos }
762 1.1 christos
763 1.1 christos /* Return true iff the thread with thread ID TID is alive. */
764 1.1 christos bool
765 1.1 christos win32_process_target::thread_alive (ptid_t ptid)
766 1.1 christos {
767 1.1 christos /* Our thread list is reliable; don't bother to poll target
768 1.1 christos threads. */
769 1.1 christos return find_thread_ptid (ptid) != NULL;
770 1.1 christos }
771 1.1 christos
772 1.1 christos /* Resume the inferior process. RESUME_INFO describes how we want
773 1.1 christos to resume. */
774 1.1 christos void
775 1.1 christos win32_process_target::resume (thread_resume *resume_info, size_t n)
776 1.1 christos {
777 1.1 christos DWORD tid;
778 1.1 christos enum gdb_signal sig;
779 1.1 christos int step;
780 1.1 christos windows_thread_info *th;
781 1.1 christos DWORD continue_status = DBG_CONTINUE;
782 1.1 christos ptid_t ptid;
783 1.1 christos
784 1.1 christos /* This handles the very limited set of resume packets that GDB can
785 1.1 christos currently produce. */
786 1.1 christos
787 1.1 christos if (n == 1 && resume_info[0].thread == minus_one_ptid)
788 1.1 christos tid = -1;
789 1.1 christos else if (n > 1)
790 1.1 christos tid = -1;
791 1.1 christos else
792 1.1 christos /* Yes, we're ignoring resume_info[0].thread. It'd be tricky to make
793 1.1 christos the Windows resume code do the right thing for thread switching. */
794 1.1.1.2 christos tid = windows_process.current_event.dwThreadId;
795 1.1 christos
796 1.1 christos if (resume_info[0].thread != minus_one_ptid)
797 1.1 christos {
798 1.1 christos sig = gdb_signal_from_host (resume_info[0].sig);
799 1.1 christos step = resume_info[0].kind == resume_step;
800 1.1 christos }
801 1.1 christos else
802 1.1 christos {
803 1.1 christos sig = GDB_SIGNAL_0;
804 1.1 christos step = 0;
805 1.1 christos }
806 1.1 christos
807 1.1 christos if (sig != GDB_SIGNAL_0)
808 1.1 christos {
809 1.1.1.2 christos if (windows_process.current_event.dwDebugEventCode
810 1.1.1.2 christos != EXCEPTION_DEBUG_EVENT)
811 1.1 christos {
812 1.1 christos OUTMSG (("Cannot continue with signal %s here.\n",
813 1.1 christos gdb_signal_to_string (sig)));
814 1.1 christos }
815 1.1.1.2 christos else if (sig == windows_process.last_sig)
816 1.1 christos continue_status = DBG_EXCEPTION_NOT_HANDLED;
817 1.1 christos else
818 1.1 christos OUTMSG (("Can only continue with received signal %s.\n",
819 1.1.1.2 christos gdb_signal_to_string (windows_process.last_sig)));
820 1.1 christos }
821 1.1 christos
822 1.1.1.2 christos windows_process.last_sig = GDB_SIGNAL_0;
823 1.1 christos
824 1.1 christos /* Get context for the currently selected thread. */
825 1.1.1.2 christos ptid = debug_event_ptid (&windows_process.current_event);
826 1.1.1.2 christos th = windows_process.thread_rec (ptid, DONT_INVALIDATE_CONTEXT);
827 1.1 christos if (th)
828 1.1 christos {
829 1.1 christos win32_prepare_to_resume (th);
830 1.1 christos
831 1.1 christos DWORD *context_flags;
832 1.1 christos #ifdef __x86_64__
833 1.1.1.2 christos if (windows_process.wow64_process)
834 1.1 christos context_flags = &th->wow64_context.ContextFlags;
835 1.1 christos else
836 1.1 christos #endif
837 1.1 christos context_flags = &th->context.ContextFlags;
838 1.1 christos if (*context_flags)
839 1.1 christos {
840 1.1 christos /* Move register values from the inferior into the thread
841 1.1 christos context structure. */
842 1.1 christos regcache_invalidate ();
843 1.1 christos
844 1.1 christos if (step)
845 1.1 christos {
846 1.1 christos if (the_low_target.single_step != NULL)
847 1.1 christos (*the_low_target.single_step) (th);
848 1.1 christos else
849 1.1 christos error ("Single stepping is not supported "
850 1.1 christos "in this configuration.\n");
851 1.1 christos }
852 1.1 christos
853 1.1 christos win32_set_thread_context (th);
854 1.1 christos *context_flags = 0;
855 1.1 christos }
856 1.1 christos }
857 1.1 christos
858 1.1 christos /* Allow continuing with the same signal that interrupted us.
859 1.1 christos Otherwise complain. */
860 1.1 christos
861 1.1 christos child_continue (continue_status, tid);
862 1.1 christos }
863 1.1 christos
864 1.1.1.2 christos /* See nat/windows-nat.h. */
865 1.1.1.2 christos
866 1.1.1.2 christos void
867 1.1.1.2 christos gdbserver_windows_process::handle_load_dll (const char *name, LPVOID base)
868 1.1 christos {
869 1.1.1.2 christos CORE_ADDR load_addr = (CORE_ADDR) (uintptr_t) base;
870 1.1.1.2 christos
871 1.1 christos char buf[MAX_PATH + 1];
872 1.1 christos char buf2[MAX_PATH + 1];
873 1.1 christos
874 1.1 christos WIN32_FIND_DATAA w32_fd;
875 1.1 christos HANDLE h = FindFirstFileA (name, &w32_fd);
876 1.1 christos
877 1.1 christos /* The symbols in a dll are offset by 0x1000, which is the
878 1.1 christos offset from 0 of the first byte in an image - because
879 1.1 christos of the file header and the section alignment. */
880 1.1 christos load_addr += 0x1000;
881 1.1 christos
882 1.1 christos if (h == INVALID_HANDLE_VALUE)
883 1.1 christos strcpy (buf, name);
884 1.1 christos else
885 1.1 christos {
886 1.1 christos FindClose (h);
887 1.1 christos strcpy (buf, name);
888 1.1 christos {
889 1.1 christos char cwd[MAX_PATH + 1];
890 1.1 christos char *p;
891 1.1 christos if (GetCurrentDirectoryA (MAX_PATH + 1, cwd))
892 1.1 christos {
893 1.1 christos p = strrchr (buf, '\\');
894 1.1 christos if (p)
895 1.1 christos p[1] = '\0';
896 1.1 christos SetCurrentDirectoryA (buf);
897 1.1 christos GetFullPathNameA (w32_fd.cFileName, MAX_PATH, buf, &p);
898 1.1 christos SetCurrentDirectoryA (cwd);
899 1.1 christos }
900 1.1 christos }
901 1.1 christos }
902 1.1 christos
903 1.1 christos if (strcasecmp (buf, "ntdll.dll") == 0)
904 1.1 christos {
905 1.1 christos GetSystemDirectoryA (buf, sizeof (buf));
906 1.1 christos strcat (buf, "\\ntdll.dll");
907 1.1 christos }
908 1.1 christos
909 1.1 christos #ifdef __CYGWIN__
910 1.1 christos cygwin_conv_path (CCP_WIN_A_TO_POSIX, buf, buf2, sizeof (buf2));
911 1.1 christos #else
912 1.1 christos strcpy (buf2, buf);
913 1.1 christos #endif
914 1.1 christos
915 1.1 christos loaded_dll (buf2, load_addr);
916 1.1 christos }
917 1.1 christos
918 1.1 christos /* See nat/windows-nat.h. */
919 1.1 christos
920 1.1 christos void
921 1.1.1.2 christos gdbserver_windows_process::handle_unload_dll ()
922 1.1 christos {
923 1.1 christos CORE_ADDR load_addr =
924 1.1 christos (CORE_ADDR) (uintptr_t) current_event.u.UnloadDll.lpBaseOfDll;
925 1.1 christos
926 1.1 christos /* The symbols in a dll are offset by 0x1000, which is the
927 1.1 christos offset from 0 of the first byte in an image - because
928 1.1 christos of the file header and the section alignment. */
929 1.1 christos load_addr += 0x1000;
930 1.1 christos unloaded_dll (NULL, load_addr);
931 1.1 christos }
932 1.1 christos
933 1.1 christos static void
934 1.1 christos suspend_one_thread (thread_info *thread)
935 1.1 christos {
936 1.1 christos windows_thread_info *th = (windows_thread_info *) thread_target_data (thread);
937 1.1 christos
938 1.1 christos th->suspend ();
939 1.1 christos }
940 1.1 christos
941 1.1 christos static void
942 1.1 christos fake_breakpoint_event (void)
943 1.1 christos {
944 1.1 christos OUTMSG2(("fake_breakpoint_event\n"));
945 1.1 christos
946 1.1.1.2 christos windows_process.faked_breakpoint = 1;
947 1.1 christos
948 1.1.1.2 christos memset (&windows_process.current_event, 0,
949 1.1.1.2 christos sizeof (windows_process.current_event));
950 1.1.1.2 christos windows_process.current_event.dwThreadId = windows_process.main_thread_id;
951 1.1.1.2 christos windows_process.current_event.dwDebugEventCode = EXCEPTION_DEBUG_EVENT;
952 1.1.1.2 christos windows_process.current_event.u.Exception.ExceptionRecord.ExceptionCode
953 1.1 christos = EXCEPTION_BREAKPOINT;
954 1.1 christos
955 1.1 christos for_each_thread (suspend_one_thread);
956 1.1 christos }
957 1.1 christos
958 1.1 christos /* See nat/windows-nat.h. */
959 1.1 christos
960 1.1 christos bool
961 1.1.1.2 christos gdbserver_windows_process::handle_access_violation
962 1.1.1.2 christos (const EXCEPTION_RECORD *rec)
963 1.1 christos {
964 1.1 christos return false;
965 1.1 christos }
966 1.1 christos
967 1.1 christos /* A helper function that will, if needed, set
968 1.1 christos 'stopped_at_software_breakpoint' on the thread and adjust the
969 1.1 christos PC. */
970 1.1 christos
971 1.1 christos static void
972 1.1 christos maybe_adjust_pc ()
973 1.1 christos {
974 1.1 christos struct regcache *regcache = get_thread_regcache (current_thread, 1);
975 1.1 christos child_fetch_inferior_registers (regcache, -1);
976 1.1 christos
977 1.1.1.2 christos windows_thread_info *th
978 1.1.1.2 christos = windows_process.thread_rec (current_thread_ptid (),
979 1.1.1.2 christos DONT_INVALIDATE_CONTEXT);
980 1.1 christos th->stopped_at_software_breakpoint = false;
981 1.1 christos
982 1.1.1.2 christos if (windows_process.current_event.dwDebugEventCode == EXCEPTION_DEBUG_EVENT
983 1.1.1.2 christos && ((windows_process.current_event.u.Exception.ExceptionRecord.ExceptionCode
984 1.1 christos == EXCEPTION_BREAKPOINT)
985 1.1.1.2 christos || (windows_process.current_event.u.Exception.ExceptionRecord.ExceptionCode
986 1.1 christos == STATUS_WX86_BREAKPOINT))
987 1.1.1.2 christos && windows_process.child_initialization_done)
988 1.1 christos {
989 1.1 christos th->stopped_at_software_breakpoint = true;
990 1.1 christos CORE_ADDR pc = regcache_read_pc (regcache);
991 1.1 christos CORE_ADDR sw_breakpoint_pc = pc - the_low_target.decr_pc_after_break;
992 1.1 christos regcache_write_pc (regcache, sw_breakpoint_pc);
993 1.1 christos }
994 1.1 christos }
995 1.1 christos
996 1.1 christos /* Get the next event from the child. */
997 1.1 christos
998 1.1 christos static int
999 1.1 christos get_child_debug_event (DWORD *continue_status,
1000 1.1 christos struct target_waitstatus *ourstatus)
1001 1.1 christos {
1002 1.1 christos ptid_t ptid;
1003 1.1 christos
1004 1.1.1.2 christos windows_process.last_sig = GDB_SIGNAL_0;
1005 1.1.1.2 christos ourstatus->set_spurious ();
1006 1.1 christos *continue_status = DBG_CONTINUE;
1007 1.1 christos
1008 1.1 christos /* Check if GDB sent us an interrupt request. */
1009 1.1 christos check_remote_input_interrupt_request ();
1010 1.1 christos
1011 1.1.1.2 christos DEBUG_EVENT *current_event = &windows_process.current_event;
1012 1.1.1.2 christos
1013 1.1.1.2 christos if (windows_process.soft_interrupt_requested)
1014 1.1 christos {
1015 1.1.1.2 christos windows_process.soft_interrupt_requested = 0;
1016 1.1 christos fake_breakpoint_event ();
1017 1.1 christos goto gotevent;
1018 1.1 christos }
1019 1.1 christos
1020 1.1.1.2 christos windows_process.attaching = 0;
1021 1.1 christos {
1022 1.1.1.2 christos gdb::optional<pending_stop> stop
1023 1.1.1.2 christos = windows_process.fetch_pending_stop (debug_threads);
1024 1.1 christos if (stop.has_value ())
1025 1.1 christos {
1026 1.1 christos *ourstatus = stop->status;
1027 1.1.1.2 christos windows_process.current_event = stop->event;
1028 1.1.1.2 christos ptid = debug_event_ptid (&windows_process.current_event);
1029 1.1.1.2 christos switch_to_thread (find_thread_ptid (ptid));
1030 1.1 christos return 1;
1031 1.1 christos }
1032 1.1 christos
1033 1.1 christos /* Keep the wait time low enough for comfortable remote
1034 1.1 christos interruption, but high enough so gdbserver doesn't become a
1035 1.1 christos bottleneck. */
1036 1.1.1.2 christos if (!wait_for_debug_event (&windows_process.current_event, 250))
1037 1.1 christos {
1038 1.1 christos DWORD e = GetLastError();
1039 1.1 christos
1040 1.1 christos if (e == ERROR_PIPE_NOT_CONNECTED)
1041 1.1 christos {
1042 1.1 christos /* This will happen if the loader fails to succesfully
1043 1.1 christos load the application, e.g., if the main executable
1044 1.1 christos tries to pull in a non-existing export from a
1045 1.1 christos DLL. */
1046 1.1.1.2 christos ourstatus->set_exited (1);
1047 1.1 christos return 1;
1048 1.1 christos }
1049 1.1 christos
1050 1.1 christos return 0;
1051 1.1 christos }
1052 1.1 christos }
1053 1.1 christos
1054 1.1 christos gotevent:
1055 1.1 christos
1056 1.1.1.2 christos switch (current_event->dwDebugEventCode)
1057 1.1 christos {
1058 1.1 christos case CREATE_THREAD_DEBUG_EVENT:
1059 1.1 christos OUTMSG2 (("gdbserver: kernel event CREATE_THREAD_DEBUG_EVENT "
1060 1.1 christos "for pid=%u tid=%x)\n",
1061 1.1.1.2 christos (unsigned) current_event->dwProcessId,
1062 1.1.1.2 christos (unsigned) current_event->dwThreadId));
1063 1.1 christos
1064 1.1 christos /* Record the existence of this thread. */
1065 1.1.1.2 christos child_add_thread (current_event->dwProcessId,
1066 1.1.1.2 christos current_event->dwThreadId,
1067 1.1.1.2 christos current_event->u.CreateThread.hThread,
1068 1.1.1.2 christos current_event->u.CreateThread.lpThreadLocalBase);
1069 1.1 christos break;
1070 1.1 christos
1071 1.1 christos case EXIT_THREAD_DEBUG_EVENT:
1072 1.1 christos OUTMSG2 (("gdbserver: kernel event EXIT_THREAD_DEBUG_EVENT "
1073 1.1 christos "for pid=%u tid=%x\n",
1074 1.1.1.2 christos (unsigned) current_event->dwProcessId,
1075 1.1.1.2 christos (unsigned) current_event->dwThreadId));
1076 1.1.1.2 christos child_delete_thread (current_event->dwProcessId,
1077 1.1.1.2 christos current_event->dwThreadId);
1078 1.1 christos
1079 1.1.1.2 christos switch_to_thread (get_first_thread ());
1080 1.1 christos return 1;
1081 1.1 christos
1082 1.1 christos case CREATE_PROCESS_DEBUG_EVENT:
1083 1.1 christos OUTMSG2 (("gdbserver: kernel event CREATE_PROCESS_DEBUG_EVENT "
1084 1.1 christos "for pid=%u tid=%x\n",
1085 1.1.1.2 christos (unsigned) current_event->dwProcessId,
1086 1.1.1.2 christos (unsigned) current_event->dwThreadId));
1087 1.1.1.2 christos CloseHandle (current_event->u.CreateProcessInfo.hFile);
1088 1.1 christos
1089 1.1.1.2 christos if (windows_process.open_process_used)
1090 1.1 christos {
1091 1.1.1.2 christos CloseHandle (windows_process.handle);
1092 1.1.1.2 christos windows_process.open_process_used = false;
1093 1.1 christos }
1094 1.1 christos
1095 1.1.1.2 christos windows_process.handle = current_event->u.CreateProcessInfo.hProcess;
1096 1.1.1.2 christos windows_process.main_thread_id = current_event->dwThreadId;
1097 1.1 christos
1098 1.1 christos /* Add the main thread. */
1099 1.1.1.2 christos child_add_thread (current_event->dwProcessId,
1100 1.1.1.2 christos windows_process.main_thread_id,
1101 1.1.1.2 christos current_event->u.CreateProcessInfo.hThread,
1102 1.1.1.2 christos current_event->u.CreateProcessInfo.lpThreadLocalBase);
1103 1.1 christos break;
1104 1.1 christos
1105 1.1 christos case EXIT_PROCESS_DEBUG_EVENT:
1106 1.1 christos OUTMSG2 (("gdbserver: kernel event EXIT_PROCESS_DEBUG_EVENT "
1107 1.1 christos "for pid=%u tid=%x\n",
1108 1.1.1.2 christos (unsigned) current_event->dwProcessId,
1109 1.1.1.2 christos (unsigned) current_event->dwThreadId));
1110 1.1 christos {
1111 1.1.1.2 christos DWORD exit_status = current_event->u.ExitProcess.dwExitCode;
1112 1.1 christos /* If the exit status looks like a fatal exception, but we
1113 1.1 christos don't recognize the exception's code, make the original
1114 1.1 christos exit status value available, to avoid losing information. */
1115 1.1 christos int exit_signal
1116 1.1 christos = WIFSIGNALED (exit_status) ? WTERMSIG (exit_status) : -1;
1117 1.1 christos if (exit_signal == -1)
1118 1.1.1.2 christos ourstatus->set_exited (exit_status);
1119 1.1 christos else
1120 1.1.1.2 christos ourstatus->set_signalled (gdb_signal_from_host (exit_signal));
1121 1.1 christos }
1122 1.1.1.2 christos child_continue (DBG_CONTINUE, windows_process.desired_stop_thread_id);
1123 1.1 christos break;
1124 1.1 christos
1125 1.1 christos case LOAD_DLL_DEBUG_EVENT:
1126 1.1 christos OUTMSG2 (("gdbserver: kernel event LOAD_DLL_DEBUG_EVENT "
1127 1.1 christos "for pid=%u tid=%x\n",
1128 1.1.1.2 christos (unsigned) current_event->dwProcessId,
1129 1.1.1.2 christos (unsigned) current_event->dwThreadId));
1130 1.1.1.2 christos CloseHandle (current_event->u.LoadDll.hFile);
1131 1.1.1.2 christos if (! windows_process.child_initialization_done)
1132 1.1 christos break;
1133 1.1.1.2 christos windows_process.dll_loaded_event ();
1134 1.1 christos
1135 1.1.1.2 christos ourstatus->set_loaded ();
1136 1.1 christos break;
1137 1.1 christos
1138 1.1 christos case UNLOAD_DLL_DEBUG_EVENT:
1139 1.1 christos OUTMSG2 (("gdbserver: kernel event UNLOAD_DLL_DEBUG_EVENT "
1140 1.1 christos "for pid=%u tid=%x\n",
1141 1.1.1.2 christos (unsigned) current_event->dwProcessId,
1142 1.1.1.2 christos (unsigned) current_event->dwThreadId));
1143 1.1.1.2 christos if (! windows_process.child_initialization_done)
1144 1.1 christos break;
1145 1.1.1.2 christos windows_process.handle_unload_dll ();
1146 1.1.1.2 christos ourstatus->set_loaded ();
1147 1.1 christos break;
1148 1.1 christos
1149 1.1 christos case EXCEPTION_DEBUG_EVENT:
1150 1.1 christos OUTMSG2 (("gdbserver: kernel event EXCEPTION_DEBUG_EVENT "
1151 1.1 christos "for pid=%u tid=%x\n",
1152 1.1.1.2 christos (unsigned) current_event->dwProcessId,
1153 1.1.1.2 christos (unsigned) current_event->dwThreadId));
1154 1.1.1.2 christos if (windows_process.handle_exception (ourstatus, debug_threads)
1155 1.1 christos == HANDLE_EXCEPTION_UNHANDLED)
1156 1.1 christos *continue_status = DBG_EXCEPTION_NOT_HANDLED;
1157 1.1 christos break;
1158 1.1 christos
1159 1.1 christos case OUTPUT_DEBUG_STRING_EVENT:
1160 1.1 christos /* A message from the kernel (or Cygwin). */
1161 1.1 christos OUTMSG2 (("gdbserver: kernel event OUTPUT_DEBUG_STRING_EVENT "
1162 1.1 christos "for pid=%u tid=%x\n",
1163 1.1.1.2 christos (unsigned) current_event->dwProcessId,
1164 1.1.1.2 christos (unsigned) current_event->dwThreadId));
1165 1.1.1.2 christos windows_process.handle_output_debug_string (nullptr);
1166 1.1 christos break;
1167 1.1 christos
1168 1.1 christos default:
1169 1.1 christos OUTMSG2 (("gdbserver: kernel event unknown "
1170 1.1 christos "for pid=%u tid=%x code=%x\n",
1171 1.1.1.2 christos (unsigned) current_event->dwProcessId,
1172 1.1.1.2 christos (unsigned) current_event->dwThreadId,
1173 1.1.1.2 christos (unsigned) current_event->dwDebugEventCode));
1174 1.1 christos break;
1175 1.1 christos }
1176 1.1 christos
1177 1.1.1.2 christos ptid = debug_event_ptid (&windows_process.current_event);
1178 1.1 christos
1179 1.1.1.2 christos if (windows_process.desired_stop_thread_id != -1
1180 1.1.1.2 christos && windows_process.desired_stop_thread_id != ptid.lwp ())
1181 1.1 christos {
1182 1.1 christos /* Pending stop. See the comment by the definition of
1183 1.1 christos "pending_stops" for details on why this is needed. */
1184 1.1 christos OUTMSG2 (("get_windows_debug_event - "
1185 1.1 christos "unexpected stop in 0x%lx (expecting 0x%x)\n",
1186 1.1.1.2 christos ptid.lwp (), windows_process.desired_stop_thread_id));
1187 1.1 christos maybe_adjust_pc ();
1188 1.1.1.2 christos windows_process.pending_stops.push_back
1189 1.1.1.2 christos ({(DWORD) ptid.lwp (), *ourstatus, *current_event});
1190 1.1.1.2 christos ourstatus->set_spurious ();
1191 1.1 christos }
1192 1.1 christos else
1193 1.1.1.2 christos switch_to_thread (find_thread_ptid (ptid));
1194 1.1 christos
1195 1.1 christos return 1;
1196 1.1 christos }
1197 1.1 christos
1198 1.1 christos /* Wait for the inferior process to change state.
1199 1.1 christos STATUS will be filled in with a response code to send to GDB.
1200 1.1 christos Returns the signal which caused the process to stop. */
1201 1.1 christos ptid_t
1202 1.1 christos win32_process_target::wait (ptid_t ptid, target_waitstatus *ourstatus,
1203 1.1.1.2 christos target_wait_flags options)
1204 1.1 christos {
1205 1.1.1.2 christos if (windows_process.cached_status.kind () != TARGET_WAITKIND_IGNORE)
1206 1.1 christos {
1207 1.1 christos /* The core always does a wait after creating the inferior, and
1208 1.1 christos do_initial_child_stuff already ran the inferior to the
1209 1.1 christos initial breakpoint (or an exit, if creating the process
1210 1.1 christos fails). Report it now. */
1211 1.1.1.2 christos *ourstatus = windows_process.cached_status;
1212 1.1.1.2 christos windows_process.cached_status.set_ignore ();
1213 1.1.1.2 christos return debug_event_ptid (&windows_process.current_event);
1214 1.1 christos }
1215 1.1 christos
1216 1.1 christos while (1)
1217 1.1 christos {
1218 1.1 christos DWORD continue_status;
1219 1.1 christos if (!get_child_debug_event (&continue_status, ourstatus))
1220 1.1 christos continue;
1221 1.1 christos
1222 1.1.1.2 christos switch (ourstatus->kind ())
1223 1.1 christos {
1224 1.1 christos case TARGET_WAITKIND_EXITED:
1225 1.1 christos OUTMSG2 (("Child exited with retcode = %x\n",
1226 1.1.1.2 christos ourstatus->exit_status ()));
1227 1.1 christos win32_clear_inferiors ();
1228 1.1.1.2 christos return ptid_t (windows_process.current_event.dwProcessId);
1229 1.1 christos case TARGET_WAITKIND_STOPPED:
1230 1.1 christos case TARGET_WAITKIND_SIGNALLED:
1231 1.1 christos case TARGET_WAITKIND_LOADED:
1232 1.1 christos {
1233 1.1 christos OUTMSG2 (("Child Stopped with signal = %d \n",
1234 1.1.1.2 christos ourstatus->sig ()));
1235 1.1 christos maybe_adjust_pc ();
1236 1.1.1.2 christos return debug_event_ptid (&windows_process.current_event);
1237 1.1 christos }
1238 1.1 christos default:
1239 1.1.1.2 christos OUTMSG (("Ignoring unknown internal event, %d\n",
1240 1.1.1.2 christos ourstatus->kind ()));
1241 1.1 christos /* fall-through */
1242 1.1 christos case TARGET_WAITKIND_SPURIOUS:
1243 1.1 christos /* do nothing, just continue */
1244 1.1.1.2 christos child_continue (continue_status,
1245 1.1.1.2 christos windows_process.desired_stop_thread_id);
1246 1.1 christos break;
1247 1.1 christos }
1248 1.1 christos }
1249 1.1 christos }
1250 1.1 christos
1251 1.1 christos /* Fetch registers from the inferior process.
1252 1.1 christos If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */
1253 1.1 christos void
1254 1.1 christos win32_process_target::fetch_registers (regcache *regcache, int regno)
1255 1.1 christos {
1256 1.1 christos child_fetch_inferior_registers (regcache, regno);
1257 1.1 christos }
1258 1.1 christos
1259 1.1 christos /* Store registers to the inferior process.
1260 1.1 christos If REGNO is -1, store all registers; otherwise, store at least REGNO. */
1261 1.1 christos void
1262 1.1 christos win32_process_target::store_registers (regcache *regcache, int regno)
1263 1.1 christos {
1264 1.1 christos child_store_inferior_registers (regcache, regno);
1265 1.1 christos }
1266 1.1 christos
1267 1.1 christos /* Read memory from the inferior process. This should generally be
1268 1.1 christos called through read_inferior_memory, which handles breakpoint shadowing.
1269 1.1 christos Read LEN bytes at MEMADDR into a buffer at MYADDR. */
1270 1.1 christos int
1271 1.1 christos win32_process_target::read_memory (CORE_ADDR memaddr, unsigned char *myaddr,
1272 1.1 christos int len)
1273 1.1 christos {
1274 1.1 christos return child_xfer_memory (memaddr, (char *) myaddr, len, 0, 0) != len;
1275 1.1 christos }
1276 1.1 christos
1277 1.1 christos /* Write memory to the inferior process. This should generally be
1278 1.1 christos called through write_inferior_memory, which handles breakpoint shadowing.
1279 1.1 christos Write LEN bytes from the buffer at MYADDR to MEMADDR.
1280 1.1 christos Returns 0 on success and errno on failure. */
1281 1.1 christos int
1282 1.1 christos win32_process_target::write_memory (CORE_ADDR memaddr,
1283 1.1 christos const unsigned char *myaddr, int len)
1284 1.1 christos {
1285 1.1 christos return child_xfer_memory (memaddr, (char *) myaddr, len, 1, 0) != len;
1286 1.1 christos }
1287 1.1 christos
1288 1.1 christos /* Send an interrupt request to the inferior process. */
1289 1.1 christos void
1290 1.1 christos win32_process_target::request_interrupt ()
1291 1.1 christos {
1292 1.1.1.2 christos if (GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT, signal_pid))
1293 1.1 christos return;
1294 1.1 christos
1295 1.1 christos /* GenerateConsoleCtrlEvent can fail if process id being debugged is
1296 1.1 christos not a process group id.
1297 1.1 christos Fallback to XP/Vista 'DebugBreakProcess', which generates a
1298 1.1 christos breakpoint exception in the interior process. */
1299 1.1 christos
1300 1.1.1.2 christos if (DebugBreakProcess (windows_process.handle))
1301 1.1 christos return;
1302 1.1 christos
1303 1.1 christos /* Last resort, suspend all threads manually. */
1304 1.1.1.2 christos windows_process.soft_interrupt_requested = 1;
1305 1.1 christos }
1306 1.1 christos
1307 1.1 christos bool
1308 1.1 christos win32_process_target::supports_hardware_single_step ()
1309 1.1 christos {
1310 1.1 christos return true;
1311 1.1 christos }
1312 1.1 christos
1313 1.1 christos bool
1314 1.1 christos win32_process_target::supports_qxfer_siginfo ()
1315 1.1 christos {
1316 1.1 christos return true;
1317 1.1 christos }
1318 1.1 christos
1319 1.1 christos /* Write Windows signal info. */
1320 1.1 christos
1321 1.1 christos int
1322 1.1 christos win32_process_target::qxfer_siginfo (const char *annex,
1323 1.1 christos unsigned char *readbuf,
1324 1.1 christos unsigned const char *writebuf,
1325 1.1 christos CORE_ADDR offset, int len)
1326 1.1 christos {
1327 1.1.1.2 christos if (windows_process.siginfo_er.ExceptionCode == 0)
1328 1.1 christos return -1;
1329 1.1 christos
1330 1.1 christos if (readbuf == nullptr)
1331 1.1 christos return -1;
1332 1.1 christos
1333 1.1.1.2 christos char *buf = (char *) &windows_process.siginfo_er;
1334 1.1.1.2 christos size_t bufsize = sizeof (windows_process.siginfo_er);
1335 1.1 christos
1336 1.1 christos #ifdef __x86_64__
1337 1.1 christos EXCEPTION_RECORD32 er32;
1338 1.1.1.2 christos if (windows_process.wow64_process)
1339 1.1 christos {
1340 1.1 christos buf = (char *) &er32;
1341 1.1 christos bufsize = sizeof (er32);
1342 1.1 christos
1343 1.1.1.2 christos er32.ExceptionCode = windows_process.siginfo_er.ExceptionCode;
1344 1.1.1.2 christos er32.ExceptionFlags = windows_process.siginfo_er.ExceptionFlags;
1345 1.1.1.2 christos er32.ExceptionRecord
1346 1.1.1.2 christos = (uintptr_t) windows_process.siginfo_er.ExceptionRecord;
1347 1.1.1.2 christos er32.ExceptionAddress
1348 1.1.1.2 christos = (uintptr_t) windows_process.siginfo_er.ExceptionAddress;
1349 1.1.1.2 christos er32.NumberParameters = windows_process.siginfo_er.NumberParameters;
1350 1.1 christos int i;
1351 1.1 christos for (i = 0; i < EXCEPTION_MAXIMUM_PARAMETERS; i++)
1352 1.1.1.2 christos er32.ExceptionInformation[i]
1353 1.1.1.2 christos = windows_process.siginfo_er.ExceptionInformation[i];
1354 1.1 christos }
1355 1.1 christos #endif
1356 1.1 christos
1357 1.1 christos if (offset > bufsize)
1358 1.1 christos return -1;
1359 1.1 christos
1360 1.1 christos if (offset + len > bufsize)
1361 1.1 christos len = bufsize - offset;
1362 1.1 christos
1363 1.1 christos memcpy (readbuf, buf + offset, len);
1364 1.1 christos
1365 1.1 christos return len;
1366 1.1 christos }
1367 1.1 christos
1368 1.1 christos bool
1369 1.1 christos win32_process_target::supports_get_tib_address ()
1370 1.1 christos {
1371 1.1 christos return true;
1372 1.1 christos }
1373 1.1 christos
1374 1.1 christos /* Write Windows OS Thread Information Block address. */
1375 1.1 christos
1376 1.1 christos int
1377 1.1 christos win32_process_target::get_tib_address (ptid_t ptid, CORE_ADDR *addr)
1378 1.1 christos {
1379 1.1 christos windows_thread_info *th;
1380 1.1.1.2 christos th = windows_process.thread_rec (ptid, DONT_INVALIDATE_CONTEXT);
1381 1.1 christos if (th == NULL)
1382 1.1 christos return 0;
1383 1.1 christos if (addr != NULL)
1384 1.1 christos *addr = th->thread_local_base;
1385 1.1 christos return 1;
1386 1.1 christos }
1387 1.1 christos
1388 1.1 christos /* Implementation of the target_ops method "sw_breakpoint_from_kind". */
1389 1.1 christos
1390 1.1 christos const gdb_byte *
1391 1.1 christos win32_process_target::sw_breakpoint_from_kind (int kind, int *size)
1392 1.1 christos {
1393 1.1 christos *size = the_low_target.breakpoint_len;
1394 1.1 christos return the_low_target.breakpoint;
1395 1.1 christos }
1396 1.1 christos
1397 1.1 christos bool
1398 1.1 christos win32_process_target::stopped_by_sw_breakpoint ()
1399 1.1 christos {
1400 1.1.1.2 christos windows_thread_info *th
1401 1.1.1.2 christos = windows_process.thread_rec (current_thread_ptid (),
1402 1.1.1.2 christos DONT_INVALIDATE_CONTEXT);
1403 1.1 christos return th == nullptr ? false : th->stopped_at_software_breakpoint;
1404 1.1 christos }
1405 1.1 christos
1406 1.1 christos bool
1407 1.1 christos win32_process_target::supports_stopped_by_sw_breakpoint ()
1408 1.1 christos {
1409 1.1 christos return true;
1410 1.1 christos }
1411 1.1 christos
1412 1.1 christos CORE_ADDR
1413 1.1 christos win32_process_target::read_pc (struct regcache *regcache)
1414 1.1 christos {
1415 1.1 christos return (*the_low_target.get_pc) (regcache);
1416 1.1 christos }
1417 1.1 christos
1418 1.1 christos void
1419 1.1 christos win32_process_target::write_pc (struct regcache *regcache, CORE_ADDR pc)
1420 1.1 christos {
1421 1.1 christos return (*the_low_target.set_pc) (regcache, pc);
1422 1.1 christos }
1423 1.1 christos
1424 1.1.1.2 christos const char *
1425 1.1.1.2 christos win32_process_target::thread_name (ptid_t thread)
1426 1.1.1.2 christos {
1427 1.1.1.2 christos windows_thread_info *th
1428 1.1.1.2 christos = windows_process.thread_rec (current_thread_ptid (),
1429 1.1.1.2 christos DONT_INVALIDATE_CONTEXT);
1430 1.1.1.2 christos return th->thread_name ();
1431 1.1.1.2 christos }
1432 1.1.1.2 christos
1433 1.1.1.2 christos const char *
1434 1.1.1.2 christos win32_process_target::pid_to_exec_file (int pid)
1435 1.1.1.2 christos {
1436 1.1.1.2 christos return windows_process.pid_to_exec_file (pid);
1437 1.1.1.2 christos }
1438 1.1.1.2 christos
1439 1.1 christos /* The win32 target ops object. */
1440 1.1 christos
1441 1.1 christos static win32_process_target the_win32_target;
1442 1.1 christos
1443 1.1 christos /* Initialize the Win32 backend. */
1444 1.1 christos void
1445 1.1 christos initialize_low (void)
1446 1.1 christos {
1447 1.1 christos set_target_ops (&the_win32_target);
1448 1.1 christos the_low_target.arch_setup ();
1449 1.1 christos
1450 1.1.1.2 christos initialize_loadable ();
1451 1.1 christos }
1452