From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 1868 invoked by alias); 2 Aug 2013 02:51:50 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 1842 invoked by uid 89); 2 Aug 2013 02:51:49 -0000 X-Spam-SWARE-Status: No, score=-4.2 required=5.0 tests=AWL,BAYES_00,KHOP_RCVD_UNTRUST,KHOP_THREADED,RCVD_IN_HOSTKARMA_W,RCVD_IN_HOSTKARMA_WL,RDNS_NONE autolearn=no version=3.3.1 Received: from Unknown (HELO relay1.mentorg.com) (192.94.38.131) by sourceware.org (qpsmtpd/0.84/v0.84-167-ge50287c) with ESMTP; Fri, 02 Aug 2013 02:51:47 +0000 Received: from svr-orw-fem-01.mgc.mentorg.com ([147.34.98.93]) by relay1.mentorg.com with esmtp id 1V55St-000194-SO from Yao_Qi@mentor.com ; Thu, 01 Aug 2013 19:51:39 -0700 Received: from SVR-ORW-FEM-04.mgc.mentorg.com ([147.34.97.41]) by svr-orw-fem-01.mgc.mentorg.com over TLS secured channel with Microsoft SMTPSVC(6.0.3790.4675); Thu, 1 Aug 2013 19:51:39 -0700 Received: from qiyao.dyndns.org (147.34.91.1) by svr-orw-fem-04.mgc.mentorg.com (147.34.97.41) with Microsoft SMTP Server id 14.2.247.3; Thu, 1 Aug 2013 19:51:38 -0700 Message-ID: <51FB1E94.5050508@codesourcery.com> Date: Fri, 02 Aug 2013 02:51:00 -0000 From: Yao Qi User-Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130110 Thunderbird/17.0.2 MIME-Version: 1.0 To: Eli Zaretskii CC: Subject: Re: [PATCH 1/3] Detect GDB is in cygwin References: <1375087546-22591-1-git-send-email-yao@codesourcery.com> <1375087546-22591-2-git-send-email-yao@codesourcery.com> <83txjdxtpd.fsf@gnu.org> <51F786CB.3020700@codesourcery.com> <83a9l4xdst.fsf@gnu.org> <51FA137B.6070207@codesourcery.com> <83r4edz7z7.fsf@gnu.org> In-Reply-To: <83r4edz7z7.fsf@gnu.org> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-SW-Source: 2013-08/txt/msg00057.txt.bz2 On 08/02/2013 12:33 AM, Eli Zaretskii wrote: > Thanks. I have just one more request: can you verify that GDB will > not think it runs in a Cygwin pty when it is run from an MSYS Bash > window? (Since MSYS is a fork of an old version of Cygwin, it's > possible that it uses the same template for however it sets up the > shell window.) I run GDB in MSYS bash, and GDB thinks it runs in a Cygwin pty unfortunately. As you said, the file name template is the same. I add the following changes in using_cygwin_pty to differentiate MSYS and Cygwin. const char *msystem = getenv ("MSYSTEM"); /* Return false if environment variable "MSYSTEM" is set, because the code below can't tell GDB runs from MSYS or Cygwin. GDB shouldn't think it runs in a Cygwin pty when it actually runs from MSYS bash. */ if (msystem != NULL) return 0; the rest of the patch is unchanged. -- Yao (齐尧) gdb: 2013-08-02 Yao Qi Corinna Vinschen * configure.ac: Invoke AC_CHECK_HEADERS to check winternl.h, and ddk/ntddk.h. * config.in: Re-generated. * configure: Re-generated. * defs.h [__MINGW32__] (using_cygwin_pty): Declare. * mingw-hdep.c: Inlcude wchar.h. [HAVE_WINTERNL_H]: Include winternl.h. Define USE_NTQUERYINFORMATIONFILE. [!HAVE_WINTERNL_H] [HAVE_DDK_NTDDK_H]: Include ddk/ntddk.h. Define USE_NTQUERYINFORMATIONFILE.. [USE_NTQUERYINFORMATIONFILE] (get_filename_from_handle): New. (using_cygwin_pty): New. --- gdb/config.in | 6 +++ gdb/configure | 30 +++++++++++++++++ gdb/configure.ac | 7 ++++ gdb/defs.h | 4 ++ gdb/mingw-hdep.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 143 insertions(+), 0 deletions(-) diff --git a/gdb/config.in b/gdb/config.in index 92c2789..010146c 100644 --- a/gdb/config.in +++ b/gdb/config.in @@ -84,6 +84,9 @@ /* Define to 1 if you have the header file. */ #undef HAVE_CURSES_H +/* Define to 1 if you have the header file. */ +#undef HAVE_DDK_NTDDK_H + /* Define to 1 if you have the declaration of `ADDR_NO_RANDOMIZE', and to 0 if you don't. */ #undef HAVE_DECL_ADDR_NO_RANDOMIZE @@ -572,6 +575,9 @@ /* Define to 1 if you have the `wborder' function. */ #undef HAVE_WBORDER +/* Define to 1 if you have the header file. */ +#undef HAVE_WINTERNL_H + /* Define to 1 if `fork' works. */ #undef HAVE_WORKING_FORK diff --git a/gdb/configure b/gdb/configure index 4833297..ecfa203 100755 --- a/gdb/configure +++ b/gdb/configure @@ -9030,6 +9030,36 @@ fi done +# Check header winternl.h, if not found, check ddk/ntddk.h. +case "${host}" in + *-*-mingw*) for ac_header in winternl.h +do : + ac_fn_c_check_header_mongrel "$LINENO" "winternl.h" "ac_cv_header_winternl_h" "$ac_includes_default" +if test "x$ac_cv_header_winternl_h" = x""yes; then : + cat >>confdefs.h <<_ACEOF +#define HAVE_WINTERNL_H 1 +_ACEOF + +else + for ac_header in ddk/ntddk.h +do : + ac_fn_c_check_header_mongrel "$LINENO" "ddk/ntddk.h" "ac_cv_header_ddk_ntddk_h" "$ac_includes_default" +if test "x$ac_cv_header_ddk_ntddk_h" = x""yes; then : + cat >>confdefs.h <<_ACEOF +#define HAVE_DDK_NTDDK_H 1 +_ACEOF + +fi + +done + +fi + +done + + ;; +esac + # ------------------------- # # Checks for declarations. # # ------------------------- # diff --git a/gdb/configure.ac b/gdb/configure.ac index 48f37c8..c3df68e 100644 --- a/gdb/configure.ac +++ b/gdb/configure.ac @@ -1121,6 +1121,13 @@ AC_CHECK_HEADERS(term.h, [], [], #endif ]) +# Check header winternl.h, if not found, check ddk/ntddk.h. +case "${host}" in + *-*-mingw*) AC_CHECK_HEADERS(winternl.h, [], + [AC_CHECK_HEADERS(ddk/ntddk.h, [], [],[])], []) + ;; +esac + # ------------------------- # # Checks for declarations. # # ------------------------- # diff --git a/gdb/defs.h b/gdb/defs.h index 014d7d4..71c1c30 100644 --- a/gdb/defs.h +++ b/gdb/defs.h @@ -790,6 +790,10 @@ enum block_enum FIRST_LOCAL_BLOCK = 2 }; +#ifdef __MINGW32__ +int using_cygwin_pty (void); +#endif + #include "utils.h" #endif /* #ifndef DEFS_H */ diff --git a/gdb/mingw-hdep.c b/gdb/mingw-hdep.c index efc9848..965bda2 100644 --- a/gdb/mingw-hdep.c +++ b/gdb/mingw-hdep.c @@ -28,6 +28,19 @@ #include "readline/readline.h" #include +#include + +#ifdef HAVE_WINTERNL_H +#include +#define USE_NTQUERYINFORMATIONFILE 1 +#else + +#ifdef HAVE_DDK_NTDDK_H +#include +#define USE_NTQUERYINFORMATIONFILE 1 +#endif /*HAVE_DDK_NTDDK_H */ + +#endif /* HAVE_WINTERNL_H */ /* This event is signalled whenever an asynchronous SIGINT handler needs to perform an action in the main thread. */ @@ -265,6 +278,89 @@ gdb_call_async_signal_handler (struct async_signal_handler *handler, SetEvent (sigint_event); } +#ifdef USE_NTQUERYINFORMATIONFILE + +/* Return the file name of handle FH. */ + +static PWCHAR +get_filename_from_handle (HANDLE fh) +{ + IO_STATUS_BLOCK io; + NTSTATUS status; + long buf[66]; /* NAME_MAX + 1 + sizeof ULONG */ + PFILE_NAME_INFORMATION pfni = (PFILE_NAME_INFORMATION) buf; + static NTSTATUS (NTAPI *pNtQueryInformationFile) (HANDLE, + PIO_STATUS_BLOCK, + PVOID, ULONG, + FILE_INFORMATION_CLASS); + + /* Calling the native NT function NtQueryInformationFile is required to + support pre-Vista systems. If that's of no concern, Vista introduced + the GetFileInformationByHandleEx call with the FileNameInfo info class, + which can be used instead. */ + if (!pNtQueryInformationFile) + { + pNtQueryInformationFile = (NTSTATUS (NTAPI *)(HANDLE, PIO_STATUS_BLOCK, + PVOID, ULONG, FILE_INFORMATION_CLASS)) + GetProcAddress (GetModuleHandle ("ntdll.dll"), + "NtQueryInformationFile"); + if (pNtQueryInformationFile == NULL) + return NULL; + } + if (!NT_SUCCESS (pNtQueryInformationFile (fh, &io, pfni, sizeof buf, + FileNameInformation))) + return NULL; + + /* The filename is not guaranteed to be NUL-terminated. */ + pfni->FileName[pfni->FileNameLength / sizeof (WCHAR)] = L'\0'; + + return pfni->FileName; +} + +#endif /* USE_NTQUERYINFORMATIONFILE */ + +/* Return true if GDB is running in Cygwin pseudo-tty. */ + +int +using_cygwin_pty (void) +{ + PWCHAR cp; + /* Now fetch the underlying HANDLE of stdin. */ + HANDLE fh = (HANDLE) _get_osfhandle (fileno (stdin)); + const char *msystem = getenv ("MSYSTEM"); + + if (!fh || fh == INVALID_HANDLE_VALUE) + return 0; + + /* Return false if environment variable "MSYSTEM" is set, because + the code below can't tell GDB runs from MSYS or Cygwin. GDB + shouldn't think it runs in a Cygwin pty when it actually runs + from MSYS bash. */ + if (msystem != NULL) + return 0; + +#ifdef USE_NTQUERYINFORMATIONFILE + cp = get_filename_from_handle (fh); +#else + cp = NULL; +#endif + + /* Now check the name pattern. With pseudo-tty allocated in ssh, + the filename of handle of stdin looks like this: + + \cygwin-c5e39b7a9d22bafb-{p,t}ty1-from-master + + Without pseudo-tty allocated in ssh, the filename of handle of + stdin looks like this: + + \cygwin-c5e39b7a9d22bafb-pipe-0x14C8-0x3 + + If the file name is prefixed with "\cygwin-", GDB is running in + cygwin. */ + + return (cp != NULL && wcsncmp (cp, L"\\cygwin-", 8) == 0); +} + /* -Wmissing-prototypes */ extern initialize_file_ftype _initialize_mingw_hdep; -- 1.7.7.6