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