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