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