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