From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7868) id CC52E3858024; Sun, 13 Feb 2022 15:18:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CC52E3858024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Takashi Yano To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin] Cygwin: console: Fix console mode for non-cygwin inferior of GDB. X-Act-Checkin: newlib-cygwin X-Git-Author: Takashi Yano X-Git-Refname: refs/heads/master X-Git-Oldrev: bed1add783a13b3304b1625b962707f89e90e323 X-Git-Newrev: cb0e392903aeb4f2abe0fd06319e484d0a22938c Message-Id: <20220213151859.CC52E3858024@sourceware.org> Date: Sun, 13 Feb 2022 15:18:59 +0000 (GMT) X-BeenThere: cygwin-cvs@cygwin.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Cygwin core component git logs List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 13 Feb 2022 15:18:59 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3Dcb0e392903a= eb4f2abe0fd06319e484d0a22938c commit cb0e392903aeb4f2abe0fd06319e484d0a22938c Author: Takashi Yano Date: Sun Feb 13 12:03:45 2022 +0900 Cygwin: console: Fix console mode for non-cygwin inferior of GDB. =20 - Currently, there is no chance to change console mode for non-cygwin inferior of GDB. With this patch, the console mode is changed to tty::native in CreateProcess() and ContinueDebugEvent() hooked in fhandler_console. Diff: --- winsup/cygwin/fhandler.h | 2 + winsup/cygwin/fhandler_console.cc | 39 ++++++++++++ winsup/cygwin/fhandler_tty.cc | 128 ++++++++++++++++++++--------------= ---- 3 files changed, 107 insertions(+), 62 deletions(-) diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h index fb4747608..4e86ab58a 100644 --- a/winsup/cygwin/fhandler.h +++ b/winsup/cygwin/fhandler.h @@ -1941,6 +1941,8 @@ class fhandler_termios: public fhandler_base fh->copy_from (this); return fh; } + static bool path_iscygexec_a (LPCSTR n, LPSTR c); + static bool path_iscygexec_w (LPCWSTR n, LPWSTR c); }; =20 enum ansi_intensity diff --git a/winsup/cygwin/fhandler_console.cc b/winsup/cygwin/fhandler_con= sole.cc index da65c465e..50f350c49 100644 --- a/winsup/cygwin/fhandler_console.cc +++ b/winsup/cygwin/fhandler_console.cc @@ -32,6 +32,7 @@ details. */ #include "sync.h" #include "child_info.h" #include "cygwait.h" +#include "winf.h" =20 /* Don't make this bigger than NT_MAX_PATH as long as the temporary buffer is allocated using tmp_pathbuf!!! */ @@ -3595,10 +3596,32 @@ set_console_title (char *title) debug_printf ("title '%W'", buf); } =20 +static bool NO_COPY gdb_inferior_noncygwin =3D false; +static void +set_console_mode_to_native () +{ + cygheap_fdenum cfd (false); + while (cfd.next () >=3D 0) + if (cfd->get_major () =3D=3D DEV_CONS_MAJOR) + { + fhandler_console *cons =3D (fhandler_console *) (fhandler_base *) cfd; + if (cons->get_device () =3D=3D cons->tc ()->getntty ()) + { + termios *cons_ti =3D &((tty *) cons->tc ())->ti; + fhandler_console::set_input_mode (tty::native, cons_ti, + cons->get_handle_set ()); + fhandler_console::set_output_mode (tty::native, cons_ti, + cons->get_handle_set ()); + break; + } + } +} + #define DEF_HOOK(name) static __typeof__ (name) *name##_Orig /* CreateProcess() is hooked for GDB etc. */ DEF_HOOK (CreateProcessA); DEF_HOOK (CreateProcessW); +DEF_HOOK (ContinueDebugEvent); =20 static BOOL WINAPI CreateProcessA_Hooked @@ -3608,6 +3631,9 @@ CreateProcessA_Hooked { if (f & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS)) mutex_timeout =3D 0; /* to avoid deadlock in GDB */ + gdb_inferior_noncygwin =3D !fhandler_termios::path_iscygexec_a (n, c); + if (gdb_inferior_noncygwin) + set_console_mode_to_native (); return CreateProcessA_Orig (n, c, pa, ta, inh, f, e, d, si, pi); } =20 @@ -3619,9 +3645,21 @@ CreateProcessW_Hooked { if (f & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS)) mutex_timeout =3D 0; /* to avoid deadlock in GDB */ + gdb_inferior_noncygwin =3D !fhandler_termios::path_iscygexec_w (n, c); + if (gdb_inferior_noncygwin) + set_console_mode_to_native (); return CreateProcessW_Orig (n, c, pa, ta, inh, f, e, d, si, pi); } =20 +static BOOL WINAPI +ContinueDebugEvent_Hooked + (DWORD p, DWORD t, DWORD s) +{ + if (gdb_inferior_noncygwin) + set_console_mode_to_native (); + return ContinueDebugEvent_Orig (p, t, s); +} + void fhandler_console::fixup_after_fork_exec (bool execing) { @@ -3641,6 +3679,7 @@ fhandler_console::fixup_after_fork_exec (bool execing) /* CreateProcess() is hooked for GDB etc. */ DO_HOOK (NULL, CreateProcessA); DO_HOOK (NULL, CreateProcessW); + DO_HOOK (NULL, ContinueDebugEvent); } =20 /* Ugly workaround to create invisible console required since Windows 7. diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc index 8c9a10c23..5ba50cc73 100644 --- a/winsup/cygwin/fhandler_tty.cc +++ b/winsup/cygwin/fhandler_tty.cc @@ -157,6 +157,66 @@ set_switch_to_pcon (HANDLE *in, HANDLE *out, HANDLE *e= rr, bool iscygwin) *err =3D replace_err->get_output_handle_nat (); } =20 +static bool +path_iscygexec_a_w (LPCSTR na, LPSTR ca, LPCWSTR nw, LPWSTR cw) +{ + path_conv path; + tmp_pathbuf tp; + char *prog =3Dtp.c_get (); + if (na) + { + __small_sprintf (prog, "%s", na); + find_exec (prog, path); + } + else if (nw) + { + __small_sprintf (prog, "%W", nw); + find_exec (prog, path); + } + else + { + if (ca) + __small_sprintf (prog, "%s", ca); + else if (cw) + __small_sprintf (prog, "%W", cw); + else + return true; + char *p =3D prog; + char *p1; + do + if ((p1 =3D strchr (p, ' ')) || (p1 =3D p + strlen (p))) + { + p =3D p1; + if (*p =3D=3D ' ') + { + *p =3D '\0'; + find_exec (prog, path); + *p =3D ' '; + p ++; + } + else if (*p =3D=3D '\0') + find_exec (prog, path); + } + while (!path.exists() && *p); + } + const char *argv[] =3D {"", NULL}; /* Dummy */ + av av1; + av1.setup ("", path, "", 1, argv, false); + return path.iscygexec (); +} + +bool +fhandler_termios::path_iscygexec_a (LPCSTR n, LPSTR c) +{ + return path_iscygexec_a_w (n, c, NULL, NULL); +} + +bool +fhandler_termios::path_iscygexec_w (LPCWSTR n, LPWSTR c) +{ + return path_iscygexec_a_w (NULL, NULL, n, c); +} + static bool atexit_func_registered =3D false; static bool debug_process =3D false; =20 @@ -220,37 +280,9 @@ CreateProcessA_Hooked siov->hStdOutput =3D GetStdHandle (STD_OUTPUT_HANDLE); siov->hStdError =3D GetStdHandle (STD_ERROR_HANDLE); } - path_conv path; - tmp_pathbuf tp; - char *prog =3Dtp.c_get (); - if (n) - __small_sprintf (prog, "%s", n); - else - { - __small_sprintf (prog, "%s", c); - char *p =3D prog; - char *p1; - do - if ((p1 =3D strchr (p, ' ')) || (p1 =3D p + strlen (p))) - { - p =3D p1; - if (*p =3D=3D ' ') - { - *p =3D '\0'; - find_exec (prog, path); - *p =3D ' '; - p ++; - } - else if (*p =3D=3D '\0') - find_exec (prog, path); - } - while (!path.exists() && *p); - } - const char *argv[] =3D {"", NULL}; /* Dummy */ - av av1; - av1.setup ("", path, "", 1, argv, false); + bool path_iscygexec =3D fhandler_termios::path_iscygexec_a (n, c); set_switch_to_pcon (&siov->hStdInput, &siov->hStdOutput, &siov->hStdErro= r, - path.iscygexec ()); + path_iscygexec); BOOL ret =3D CreateProcessA_Orig (n, c, pa, ta, inh, f, e, d, siov, pi); h_gdb_process =3D pi->hProcess; DuplicateHandle (GetCurrentProcess (), h_gdb_process, @@ -259,7 +291,7 @@ CreateProcessA_Hooked debug_process =3D !!(f & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS)); if (debug_process) mutex_timeout =3D 0; /* to avoid deadlock in GDB */ - if (!atexit_func_registered && !path.iscygexec ()) + if (!atexit_func_registered && !path_iscygexec) { atexit (atexit_func); atexit_func_registered =3D true; @@ -286,37 +318,9 @@ CreateProcessW_Hooked siov->hStdOutput =3D GetStdHandle (STD_OUTPUT_HANDLE); siov->hStdError =3D GetStdHandle (STD_ERROR_HANDLE); } - path_conv path; - tmp_pathbuf tp; - char *prog =3Dtp.c_get (); - if (n) - __small_sprintf (prog, "%W", n); - else - { - __small_sprintf (prog, "%W", c); - char *p =3D prog; - char *p1; - do - if ((p1 =3D strchr (p, ' ')) || (p1 =3D p + strlen (p))) - { - p =3D p1; - if (*p =3D=3D ' ') - { - *p =3D '\0'; - find_exec (prog, path); - *p =3D ' '; - p ++; - } - else if (*p =3D=3D '\0') - find_exec (prog, path); - } - while (!path.exists() && *p); - } - const char *argv[] =3D {"", NULL}; /* Dummy */ - av av1; - av1.setup ("", path, "", 1, argv, false); + bool path_iscygexec =3D fhandler_termios::path_iscygexec_w (n, c); set_switch_to_pcon (&siov->hStdInput, &siov->hStdOutput, &siov->hStdErro= r, - path.iscygexec ()); + path_iscygexec); BOOL ret =3D CreateProcessW_Orig (n, c, pa, ta, inh, f, e, d, siov, pi); h_gdb_process =3D pi->hProcess; DuplicateHandle (GetCurrentProcess (), h_gdb_process, @@ -325,7 +329,7 @@ CreateProcessW_Hooked debug_process =3D !!(f & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS)); if (debug_process) mutex_timeout =3D 0; /* to avoid deadlock in GDB */ - if (!atexit_func_registered && !path.iscygexec ()) + if (!atexit_func_registered && !path_iscygexec) { atexit (atexit_func); atexit_func_registered =3D true;