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