From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 21179 invoked by alias); 25 Jul 2013 07:34:36 -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 21170 invoked by uid 89); 25 Jul 2013 07:34:35 -0000 X-Spam-SWARE-Status: No, score=0.0 required=5.0 tests=AWL,BAYES_50,RDNS_NONE,TW_SF autolearn=no version=3.3.1 Received: from Unknown (HELO calimero.vinschen.de) (217.91.18.234) by sourceware.org (qpsmtpd/0.84/v0.84-167-ge50287c) with ESMTP; Thu, 25 Jul 2013 07:34:34 +0000 Received: by calimero.vinschen.de (Postfix, from userid 500) id EB2745200A1; Thu, 25 Jul 2013 09:34:24 +0200 (CEST) Date: Thu, 25 Jul 2013 07:34:00 -0000 From: Corinna Vinschen To: gdb-patches@sourceware.org Subject: Re: [PATCH 1/3] New option --cygwin-tty. Message-ID: <20130725073424.GA8823@calimero.vinschen.de> Reply-To: gdb-patches@sourceware.org Mail-Followup-To: gdb-patches@sourceware.org References: <1374728963-25187-1-git-send-email-yao@codesourcery.com> <1374728963-25187-2-git-send-email-yao@codesourcery.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="WIyZ46R2i8wDzkSu" Content-Disposition: inline In-Reply-To: <1374728963-25187-2-git-send-email-yao@codesourcery.com> User-Agent: Mutt/1.5.21 (2010-09-15) X-Virus-Found: No X-SW-Source: 2013-07/txt/msg00598.txt.bz2 --WIyZ46R2i8wDzkSu Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-length: 786 On Jul 25 13:09, Yao Qi wrote: > Hi, > This patch is to add a new GDB option '--cygwin-tty', which is useful > when running/testing mingw32 native GDB in cygwin. Due the odd TTY > in Cygwin, the output of mingw32 native GDB behaves differently, > which causes the testsuite result unusable. I tried different > approaches to detect whether GDB is running in cygwin tty, but failed. Ptys in Cygwin are implemented as named pipes. Therefore the native MSVCRT _isatty() call returns 0 when called on a stream connected to a pty. Last year I created an isatty replacement implementation for non-Cygwin applications for demonstration. It should work with Mingw-w64 as well as VC++. I attached it to this mail, maybe it helps. Corinna -- Corinna Vinschen Cygwin Maintainer Red Hat --WIyZ46R2i8wDzkSu Content-Type: text/plain; charset=utf-8 Content-Disposition: attachment; filename="isatty-for-native-apps.c" Content-length: 3010 #include #include #include #include #include #include #ifndef __MINGW64_VERSION_MAJOR /* MS winternl.h defines FILE_INFORMATION_CLASS, but with only a different single member. */ enum FILE_INFORMATION_CLASSX { FileNameInformation = 9 }; typedef struct _FILE_NAME_INFORMATION { ULONG FileNameLength; WCHAR FileName[1]; } FILE_NAME_INFORMATION, *PFILE_NAME_INFORMATION; NTSTATUS (NTAPI *pNtQueryInformationFile) (HANDLE, PIO_STATUS_BLOCK, PVOID, ULONG, FILE_INFORMATION_CLASSX); #else NTSTATUS (NTAPI *pNtQueryInformationFile) (HANDLE, PIO_STATUS_BLOCK, PVOID, ULONG, FILE_INFORMATION_CLASS); #endif int isatty (int fd) { HANDLE fh; NTSTATUS status; IO_STATUS_BLOCK io; long buf[66]; /* NAME_MAX + 1 + sizeof ULONG */ PFILE_NAME_INFORMATION pfni = (PFILE_NAME_INFORMATION) buf; PWCHAR cp; /* First check using _isatty. Note that this returns the wrong result for NUL, for instance! Workaround is not to use _isatty at all, but rather GetFileType plus object name checking. */ if (_isatty (fd)) return 1; /* Now fetch the underlying HANDLE. */ fh = (HANDLE) _get_osfhandle (fd); if (!fh || fh == INVALID_HANDLE_VALUE) { errno = EBADF; return 0; } /* Must be a pipe. */ if (GetFileType (fh) != FILE_TYPE_PIPE) goto no_tty; /* 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) goto no_tty; } if (!NT_SUCCESS (pNtQueryInformationFile (fh, &io, pfni, sizeof buf, FileNameInformation))) goto no_tty; /* The filename is not guaranteed to be NUL-terminated. */ pfni->FileName[pfni->FileNameLength / sizeof (WCHAR)] = L'\0'; /* Now check the name pattern. The filename of a Cygwin pseudo tty pipe looks like this: \cygwin-%16llx-pty%d-{to,from}-master %16llx is the hash of the Cygwin installation, (to support multiple parallel installations), %d id the pseudo tty number, "to" or "from" differs the pipe direction. "from" is a stdin, "to" a stdout-like pipe. */ cp = pfni->FileName; if (!wcsncmp (cp, L"\\cygwin-", 8) && !wcsncmp (cp + 24, L"-pty", 4)) { cp = wcschr (cp + 28, '-'); if (!cp) goto no_tty; if (!wcscmp (cp, L"-from-master") || !wcscmp (cp, L"-to-master")) return 1; } no_tty: errno = EINVAL; return 0; } int main () { if (isatty(0)) printf("tty\n"); else printf("not a tty\n"); return 0; } --WIyZ46R2i8wDzkSu--