From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 24817 invoked by alias); 3 Feb 2006 21:55:01 -0000 Received: (qmail 24782 invoked by uid 22791); 3 Feb 2006 21:54:59 -0000 X-Spam-Check-By: sourceware.org Received: from nevyn.them.org (HELO nevyn.them.org) (66.93.172.17) by sourceware.org (qpsmtpd/0.31.1) with ESMTP; Fri, 03 Feb 2006 21:54:57 +0000 Received: from drow by nevyn.them.org with local (Exim 4.54) id 1F58tL-0000vf-6I for gdb-patches@sourceware.org; Fri, 03 Feb 2006 16:54:55 -0500 Date: Fri, 03 Feb 2006 21:55:00 -0000 From: Daniel Jacobowitz To: gdb-patches@sourceware.org Subject: RFA: Support Windows extended error numbers in safe_strerror Message-ID: <20060203215455.GA3501@nevyn.them.org> Mail-Followup-To: gdb-patches@sourceware.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.8i X-IsSubscribed: yes Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2006-02/txt/msg00047.txt.bz2 This is an improved version of a patch Mark Mitchell submitted last year. If you give strerror() anything above 42 (sys_nerr) on Windows, it gives you back "Unknown error" - particularly unfortunate since WSAECONNREFUSED is way above there, so connecting to a closed socket will give you a generic error message. This patch lets us try an OS-specific interface to fetch an error string. [Actually you need my next patch too to get the connection refused message; right now you'll get a timeout.] Any comments on this patch? -- Daniel Jacobowitz CodeSourcery 2006-02-03 Daniel Jacobowitz * utils.c (safe_strerror): Try to use FormatMessage for otherwise unknown messages on Windows. Index: src/gdb/utils.c =================================================================== --- src.orig/gdb/utils.c 2006-02-03 15:13:03.000000000 -0500 +++ src/gdb/utils.c 2006-02-03 15:16:24.000000000 -0500 @@ -1,8 +1,8 @@ /* General utility routines for GDB, the GNU debugger. Copyright (C) 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, - 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free - Software Foundation, Inc. + 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 + Free Software Foundation, Inc. This file is part of GDB. @@ -36,6 +36,10 @@ #include #endif +#ifdef USE_WIN32API +#include +#endif + /* SunOS's curses.h has a '#define reg register' in it. Thank you Sun. */ #ifdef reg #undef reg @@ -847,7 +851,34 @@ safe_strerror (int errnum) { char *msg; - msg = strerror (errnum); +#ifdef USE_WIN32API + /* On Windows, strerror never returns NULL, but it returns a useless + string for anything above sys_nerr. Try a little harder to find + a system-provided error message in that case. */ + if (errnum >= sys_nerr) + { + static char *buffer; + + if (buffer) + LocalFree (buffer); + + if (FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER + | FORMAT_MESSAGE_FROM_SYSTEM, + NULL, errnum, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPTSTR) &buffer, 0, NULL) != 0) + { + if (strcmp (buffer + strlen (buffer) - 3, ".\r\n") == 0) + buffer[strlen (buffer) - 3] = '\0'; + return buffer; + } + else + msg = NULL; + } + else +#endif + msg = strerror (errnum); + if (msg == NULL) { static char buf[32];