X-Git-Url: http://drtracing.org/?a=blobdiff_plain;f=gdb%2Fwindows-nat.c;h=3431181a88cb630fc19dd8b68e9dfc37bb636a74;hb=268a13a5a3f7c6b9b6ffc5ac2d1b24eb41f3fbdc;hp=5050e96fd126e78eb7f59b3ae0915c54b6e46ca3;hpb=0baae8dbd38534c0f3dcedc7b9204bbc8f18de1b;p=deliverable%2Fbinutils-gdb.git diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c index 5050e96fd1..3431181a88 100644 --- a/gdb/windows-nat.c +++ b/gdb/windows-nat.c @@ -1,6 +1,6 @@ /* Target-vector operations for controlling windows child processes, for GDB. - Copyright (C) 1995-2018 Free Software Foundation, Inc. + Copyright (C) 1995-2019 Free Software Foundation, Inc. Contributed by Cygnus Solutions, A Red Hat Company. @@ -67,7 +67,8 @@ #include "x86-nat.h" #include "complaints.h" #include "inf-child.h" -#include "gdb_tilde_expand.h" +#include "gdbsupport/gdb_tilde_expand.h" +#include "gdbsupport/pathstuff.h" #define AdjustTokenPrivileges dyn_AdjustTokenPrivileges #define DebugActiveProcessStop dyn_DebugActiveProcessStop @@ -146,8 +147,10 @@ static GetConsoleFontSize_ftype *GetConsoleFontSize; static int have_saved_context; /* True if we've saved context from a cygwin signal. */ +#ifdef __CYGWIN__ static CONTEXT saved_context; /* Containes the saved context from a cygwin signal. */ +#endif /* If we're not using the old Cygwin header file set, define the following which never should have been in the generic Win32 API @@ -234,7 +237,6 @@ static DEBUG_EVENT current_event; /* The current debug event from WaitForDebugEvent */ static HANDLE current_process_handle; /* Currently executing process */ static windows_thread_info *current_thread; /* Info on currently selected thread */ -static DWORD main_thread_id; /* Thread ID of the main thread */ /* Counts of things. */ static int exception_count = 0; @@ -278,26 +280,29 @@ static const int *mappings; a segment register or not. */ static segment_register_p_ftype *segment_register_p; +/* See windows_nat_target::resume to understand why this is commented + out. */ +#if 0 /* This vector maps the target's idea of an exception (extracted from the DEBUG_EVENT structure) to GDB's idea. */ struct xlate_exception { - int them; + DWORD them; enum gdb_signal us; }; -static const struct xlate_exception - xlate[] = +static const struct xlate_exception xlate[] = { {EXCEPTION_ACCESS_VIOLATION, GDB_SIGNAL_SEGV}, {STATUS_STACK_OVERFLOW, GDB_SIGNAL_SEGV}, {EXCEPTION_BREAKPOINT, GDB_SIGNAL_TRAP}, {DBG_CONTROL_C, GDB_SIGNAL_INT}, {EXCEPTION_SINGLE_STEP, GDB_SIGNAL_TRAP}, - {STATUS_FLOAT_DIVIDE_BY_ZERO, GDB_SIGNAL_FPE}, - {-1, GDB_SIGNAL_UNKNOWN}}; + {STATUS_FLOAT_DIVIDE_BY_ZERO, GDB_SIGNAL_FPE} +}; +#endif /* 0 */ struct windows_nat_target final : public x86_nat_target { @@ -335,7 +340,7 @@ struct windows_nat_target final : public x86_nat_target bool thread_alive (ptid_t ptid) override; - const char *pid_to_str (ptid_t) override; + std::string pid_to_str (ptid_t) override; void interrupt () override; @@ -420,9 +425,16 @@ thread_rec (DWORD id, int get_context) return NULL; } -/* Add a thread to the thread list. */ +/* Add a thread to the thread list. + + PTID is the ptid of the thread to be added. + H is its Windows handle. + TLB is its thread local base. + MAIN_THREAD_P should be true if the thread to be added is + the main thread, false otherwise. */ + static windows_thread_info * -windows_add_thread (ptid_t ptid, HANDLE h, void *tlb) +windows_add_thread (ptid_t ptid, HANDLE h, void *tlb, bool main_thread_p) { windows_thread_info *th; DWORD id; @@ -440,7 +452,17 @@ windows_add_thread (ptid_t ptid, HANDLE h, void *tlb) th->thread_local_base = (CORE_ADDR) (uintptr_t) tlb; th->next = thread_head.next; thread_head.next = th; - add_thread (ptid); + + /* Add this new thread to the list of threads. + + To be consistent with what's done on other platforms, we add + the main thread silently (in reality, this thread is really + more of a process to the user than a thread). */ + if (main_thread_p) + add_thread_silent (ptid); + else + add_thread (ptid); + /* Set the debug registers for the new thread if they are used. */ if (debug_registers_used) { @@ -477,9 +499,15 @@ windows_init_thread_list (void) thread_head.next = NULL; } -/* Delete a thread from the list of threads. */ +/* Delete a thread from the list of threads. + + PTID is the ptid of the thread to be deleted. + EXIT_CODE is the thread's exit code. + MAIN_THREAD_P should be true if the thread to be deleted is + the main thread, false otherwise. */ + static void -windows_delete_thread (ptid_t ptid, DWORD exit_code) +windows_delete_thread (ptid_t ptid, DWORD exit_code, bool main_thread_p) { windows_thread_info *th; DWORD id; @@ -488,11 +516,20 @@ windows_delete_thread (ptid_t ptid, DWORD exit_code) id = ptid.tid (); + /* Emit a notification about the thread being deleted. + + Note that no notification was printed when the main thread + was created, and thus, unless in verbose mode, we should be + symetrical, and avoid that notification for the main thread + here as well. */ + if (info_verbose) - printf_unfiltered ("[Deleting %s]\n", target_pid_to_str (ptid)); - else if (print_thread_events && id != main_thread_id) + printf_unfiltered ("[Deleting %s]\n", target_pid_to_str (ptid).c_str ()); + else if (print_thread_events && !main_thread_p) printf_unfiltered (_("[%s exited with code %u]\n"), - target_pid_to_str (ptid), (unsigned) exit_code); + target_pid_to_str (ptid).c_str (), + (unsigned) exit_code); + delete_thread (find_thread_ptid (ptid)); for (th = &thread_head; @@ -740,7 +777,6 @@ windows_make_so (const char *name, LPVOID load_addr) if (p >= so->so_name && strcasecmp (p, "/cygwin1.dll") == 0) { asection *text = NULL; - CORE_ADDR text_vma; gdb_bfd_ref_ptr abfd (gdb_bfd_open (so->so_name, "pei-i386", -1)); @@ -906,15 +942,14 @@ handle_unload_dll () static void catch_errors (void (*func) ()) { - TRY + try { func (); } - CATCH (ex, RETURN_MASK_ALL) + catch (const gdb_exception &ex) { exception_print (gdb_stderr, ex); } - END_CATCH } /* Clear list of loaded DLLs. */ @@ -994,7 +1029,7 @@ handle_output_debug_string (struct target_waitstatus *ourstatus) ourstatus->kind = TARGET_WAITKIND_STOPPED; retval = strtoul (p, &p, 0); if (!retval) - retval = main_thread_id; + retval = current_event.dwThreadId; else if ((x = (LPCVOID) (uintptr_t) strtoull (p, NULL, 0)) && ReadProcessMemory (current_process_handle, x, &saved_context, @@ -1059,14 +1094,16 @@ display_selector (HANDLE thread, DWORD sel) puts_filtered ("Code (Exec/Read, Conf"); break; default: - printf_filtered ("Unknown type 0x%x",info.HighWord.Bits.Type); + printf_filtered ("Unknown type 0x%lx", + (unsigned long) info.HighWord.Bits.Type); } if ((info.HighWord.Bits.Type & 0x1) == 0) puts_filtered(", N.Acc"); puts_filtered (")\n"); if ((info.HighWord.Bits.Type & 0x10) == 0) puts_filtered("System selector "); - printf_filtered ("Priviledge level = %d. ", info.HighWord.Bits.Dpl); + printf_filtered ("Priviledge level = %ld. ", + (unsigned long) info.HighWord.Bits.Dpl); if (info.HighWord.Bits.Granularity) puts_filtered ("Page granular.\n"); else @@ -1298,7 +1335,6 @@ handle_exception (struct target_waitstatus *ourstatus) static BOOL windows_continue (DWORD continue_status, int id, int killed) { - int i; windows_thread_info *th; BOOL res; @@ -1369,13 +1405,13 @@ fake_create_process (void) (unsigned) GetLastError ()); /* We can not debug anything in that case. */ } - main_thread_id = current_event.dwThreadId; - current_thread = windows_add_thread ( - ptid_t (current_event.dwProcessId, 0, - current_event.dwThreadId), - current_event.u.CreateThread.hThread, - current_event.u.CreateThread.lpThreadLocalBase); - return main_thread_id; + current_thread + = windows_add_thread (ptid_t (current_event.dwProcessId, 0, + current_event.dwThreadId), + current_event.u.CreateThread.hThread, + current_event.u.CreateThread.lpThreadLocalBase, + true /* main_thread_p */); + return current_event.dwThreadId; } void @@ -1407,12 +1443,11 @@ windows_nat_target::resume (ptid_t ptid, int step, enum gdb_signal sig) structure when passing the exception to the inferior. Note that this seems possible in the exception handler itself. */ { - int i; - for (i = 0; xlate[i].them != -1; i++) - if (xlate[i].us == sig) + for (const xlate_exception &x : xlate) + if (x.us == sig) { current_event.u.Exception.ExceptionRecord.ExceptionCode - = xlate[i].them; + = x.them; continue_status = DBG_EXCEPTION_NOT_HANDLED; break; } @@ -1544,10 +1579,11 @@ get_windows_debug_event (struct target_ops *ops, } /* Record the existence of this thread. */ thread_id = current_event.dwThreadId; - th = windows_add_thread (ptid_t (current_event.dwProcessId, 0, - current_event.dwThreadId), - current_event.u.CreateThread.hThread, - current_event.u.CreateThread.lpThreadLocalBase); + th = windows_add_thread + (ptid_t (current_event.dwProcessId, 0, current_event.dwThreadId), + current_event.u.CreateThread.hThread, + current_event.u.CreateThread.lpThreadLocalBase, + false /* main_thread_p */); break; @@ -1556,14 +1592,11 @@ get_windows_debug_event (struct target_ops *ops, (unsigned) current_event.dwProcessId, (unsigned) current_event.dwThreadId, "EXIT_THREAD_DEBUG_EVENT")); - - if (current_event.dwThreadId != main_thread_id) - { - windows_delete_thread (ptid_t (current_event.dwProcessId, 0, - current_event.dwThreadId), - current_event.u.ExitThread.dwExitCode); - th = &dummy_thread_info; - } + windows_delete_thread (ptid_t (current_event.dwProcessId, 0, + current_event.dwThreadId), + current_event.u.ExitThread.dwExitCode, + false /* main_thread_p */); + th = &dummy_thread_info; break; case CREATE_PROCESS_DEBUG_EVENT: @@ -1576,16 +1609,13 @@ get_windows_debug_event (struct target_ops *ops, break; current_process_handle = current_event.u.CreateProcessInfo.hProcess; - if (main_thread_id) - windows_delete_thread (ptid_t (current_event.dwProcessId, 0, - main_thread_id), - 0); - main_thread_id = current_event.dwThreadId; /* Add the main thread. */ - th = windows_add_thread (ptid_t (current_event.dwProcessId, 0, - current_event.dwThreadId), - current_event.u.CreateProcessInfo.hThread, - current_event.u.CreateProcessInfo.lpThreadLocalBase); + th = windows_add_thread + (ptid_t (current_event.dwProcessId, 0, + current_event.dwThreadId), + current_event.u.CreateProcessInfo.hThread, + current_event.u.CreateProcessInfo.lpThreadLocalBase, + true /* main_thread_p */); thread_id = current_event.dwThreadId; break; @@ -1603,9 +1633,12 @@ get_windows_debug_event (struct target_ops *ops, } else if (saw_create == 1) { + windows_delete_thread (ptid_t (current_event.dwProcessId, 0, + current_event.dwThreadId), + 0, true /* main_thread_p */); ourstatus->kind = TARGET_WAITKIND_EXITED; ourstatus->value.integer = current_event.u.ExitProcess.dwExitCode; - thread_id = main_thread_id; + thread_id = current_event.dwThreadId; } break; @@ -1620,7 +1653,7 @@ get_windows_debug_event (struct target_ops *ops, catch_errors (handle_load_dll); ourstatus->kind = TARGET_WAITKIND_LOADED; ourstatus->value.integer = 0; - thread_id = main_thread_id; + thread_id = current_event.dwThreadId; break; case UNLOAD_DLL_DEBUG_EVENT: @@ -1633,7 +1666,7 @@ get_windows_debug_event (struct target_ops *ops, catch_errors (handle_unload_dll); ourstatus->kind = TARGET_WAITKIND_LOADED; ourstatus->value.integer = 0; - thread_id = main_thread_id; + thread_id = current_event.dwThreadId; break; case EXCEPTION_DEBUG_EVENT: @@ -1702,8 +1735,6 @@ windows_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus, { int pid = -1; - target_terminal::ours (); - /* We loop when we get a non-standard exception rather than return with a SPURIOUS because resume can try and step or modify things, which needs a current_thread->h. But some of these exceptions mark @@ -1765,7 +1796,6 @@ windows_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus, static void windows_add_all_dlls (void) { - struct so_list *so; HMODULE dummy_hmodule; DWORD cb_needed; HMODULE *hmodules; @@ -1815,7 +1845,6 @@ do_initial_windows_stuff (struct target_ops *ops, DWORD pid, int attaching) { int i; struct inferior *inf; - struct thread_info *tp; last_sig = GDB_SIGNAL_0; event_count = 0; @@ -1980,12 +2009,10 @@ windows_nat_target::attach (const char *args, int from_tty) if (exec_file) printf_unfiltered ("Attaching to program `%s', %s\n", exec_file, - target_pid_to_str (ptid_t (pid))); + target_pid_to_str (ptid_t (pid)).c_str ()); else printf_unfiltered ("Attaching to %s\n", - target_pid_to_str (ptid_t (pid))); - - gdb_flush (gdb_stdout); + target_pid_to_str (ptid_t (pid)).c_str ()); } do_initial_windows_stuff (this, pid, 1); @@ -2015,7 +2042,6 @@ windows_nat_target::detach (inferior *inf, int from_tty) exec_file = ""; printf_unfiltered ("Detaching from program: %s, Pid %u\n", exec_file, (unsigned) current_event.dwProcessId); - gdb_flush (gdb_stdout); } x86_cleanup_dregs (); @@ -2114,7 +2140,7 @@ windows_nat_target::files_info () printf_unfiltered ("\tUsing the running image of %s %s.\n", inf->attach_flag ? "attached" : "child", - target_pid_to_str (inferior_ptid)); + target_pid_to_str (inferior_ptid).c_str ()); } /* Modify CreateProcess parameters for use of a new separate console. @@ -2516,16 +2542,12 @@ windows_nat_target::create_inferior (const char *exec_file, int tty; int ostdin, ostdout, ostderr; #else /* !__CYGWIN__ */ - char real_path[__PMAX]; char shell[__PMAX]; /* Path to shell */ const char *toexec; char *args, *allargs_copy; size_t args_len, allargs_len; int fd_inp = -1, fd_out = -1, fd_err = -1; HANDLE tty = INVALID_HANDLE_VALUE; - HANDLE inf_stdin = INVALID_HANDLE_VALUE; - HANDLE inf_stdout = INVALID_HANDLE_VALUE; - HANDLE inf_stderr = INVALID_HANDLE_VALUE; bool redirected = false; char *w32env; char *temp; @@ -2583,9 +2605,7 @@ windows_nat_target::create_inferior (const char *exec_file, } else { - sh = getenv ("SHELL"); - if (!sh) - sh = "/bin/sh"; + sh = get_shell (); if (cygwin_conv_path (CCP_POSIX_TO_WIN_W, sh, shell, __PMAX) < 0) error (_("Error starting executable via shell: %d"), errno); #ifdef __USEWIDE @@ -2686,13 +2706,13 @@ windows_nat_target::create_inferior (const char *exec_file, if (tty >= 0) { - close (tty); + ::close (tty); dup2 (ostdin, 0); dup2 (ostdout, 1); dup2 (ostderr, 2); - close (ostdin); - close (ostdout); - close (ostderr); + ::close (ostdin); + ::close (ostdout); + ::close (ostderr); } #else /* !__CYGWIN__ */ allargs_len = strlen (allargs); @@ -2912,17 +2932,11 @@ windows_nat_target::close () } /* Convert pid to printable format. */ -const char * +std::string windows_nat_target::pid_to_str (ptid_t ptid) { - static char buf[80]; - if (ptid.tid () != 0) - { - snprintf (buf, sizeof (buf), "Thread %d.0x%lx", - ptid.pid (), ptid.tid ()); - return buf; - } + return string_printf ("Thread %d.0x%lx", ptid.pid (), ptid.tid ()); return normal_pid_to_str (ptid); }