From patchwork Fri Jan 9 22:19:02 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergio Durigan Junior X-Patchwork-Id: 4600 Received: (qmail 19742 invoked by alias); 9 Jan 2015 22:19:16 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 19598 invoked by uid 89); 9 Jan 2015 22:19:15 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.7 required=5.0 tests=AWL, BAYES_50, SPF_HELO_PASS, SPF_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Fri, 09 Jan 2015 22:19:11 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id t09MJAMf016171 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL) for ; Fri, 9 Jan 2015 17:19:10 -0500 Received: from psique.yyz.redhat.com (dhcp-10-15-16-169.yyz.redhat.com [10.15.16.169]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t09MJ9Pt025112; Fri, 9 Jan 2015 17:19:09 -0500 From: Sergio Durigan Junior To: GDB Patches Cc: Pedro Alves , Gary Benson , Sergio Durigan Junior Subject: [PATCH 1/2] Move safe_strerror to common/ Date: Fri, 9 Jan 2015 17:19:02 -0500 Message-Id: <1420841943-24290-2-git-send-email-sergiodj@redhat.com> In-Reply-To: <1420841943-24290-1-git-send-email-sergiodj@redhat.com> References: <1420841943-24290-1-git-send-email-sergiodj@redhat.com> X-IsSubscribed: yes This patch moves safe_strerror from the gdb/{posix,mingw}-hdep.c files to the respective common/{posix,mingw}-strerror.c files. This is a preparation for the next patch, which shares a common code (to disable address space randomization when creating a new inferior). The patch has been regtested on Fedora 20 x86_64, and no regressions were found. I am assuming the MingW modifications are also safe, but I have not checked them too deep. gdb/ChangeLog 2015-01-09 Sergio Durigan Junior * Makefile.in (ALLDEPFILES): Including common/mingw-strerror.c and common/posix-strerror.c. (posix-strerror.o): New rule. (mingw-strerror.o): Likewise. * common/common-utils.h (safe_strerror): Move prototype to here, from utils.h. * common/common.host: New file. * common/mingw-strerror.c: Likewise. * common/posix-strerror.c: Likewise. * configure: Regenerated. * configure.ac: Add posix-strerror.o to the $gdb_host_obs variable. Source common/common.host. * configure.host: Remove variable gdb_host_obs (moved to common/common.host). * contrib/ari/gdb_ari.sh: Mention gdb/common/mingw-strerror.c and gdb/common/posix-strerror.c when warning about the use of strerror. * mingw-hdep.c (safe_strerror): Remove definition; move it to common/mingw-strerror.c. * posix-hdep.c (safe_strerror): Remove definition; move it to common/posix-hdep.c. * utils.h (safe_strerror): Remove prototype; move to common/common-utils.h. gdb/gdbserver/ChangeLog 2015-01-09 Sergio Durigan Junior * configure: Regenerated. * configure.ac: Initialize new variable $srv_host_obs. Source file ../common/common.host. Add $srv_host_obs to GDBSERVER_DEPFILES. --- gdb/Makefile.in | 12 +++++++-- gdb/common/common-utils.h | 6 +++++ gdb/common/common.host | 17 ++++++++++++ gdb/common/mingw-strerror.c | 64 +++++++++++++++++++++++++++++++++++++++++++++ gdb/common/posix-strerror.c | 38 +++++++++++++++++++++++++++ gdb/configure | 3 ++- gdb/configure.ac | 3 ++- gdb/configure.host | 3 --- gdb/contrib/ari/gdb_ari.sh | 4 +-- gdb/gdbserver/Makefile.in | 6 +++++ gdb/gdbserver/configure | 7 ++++- gdb/gdbserver/configure.ac | 7 ++++- gdb/mingw-hdep.c | 44 ------------------------------- gdb/posix-hdep.c | 20 -------------- gdb/utils.h | 3 --- 15 files changed, 159 insertions(+), 78 deletions(-) create mode 100644 gdb/common/common.host create mode 100644 gdb/common/mingw-strerror.c create mode 100644 gdb/common/posix-strerror.c diff --git a/gdb/Makefile.in b/gdb/Makefile.in index 5dae3e6..a8bae82 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -1680,7 +1680,7 @@ ALLDEPFILES = \ m68klinux-nat.c m68klinux-tdep.c \ m88k-tdep.c m88kbsd-nat.c \ microblaze-tdep.c microblaze-linux-tdep.c \ - mingw-hdep.c \ + mingw-hdep.c common/mingw-strerror.c \ mips-linux-nat.c mips-linux-tdep.c \ mips-sde-tdep.c \ mips-tdep.c \ @@ -1690,7 +1690,7 @@ ALLDEPFILES = \ nios2-tdep.c nios2-linux-tdep.c \ nbsd-nat.c nbsd-tdep.c obsd-nat.c obsd-tdep.c \ somread.c solib-som.c \ - posix-hdep.c \ + posix-hdep.c common/posix-strerror.c \ ppc-sysv-tdep.c ppc-linux-nat.c ppc-linux-tdep.c ppc64-tdep.c \ ppcfbsd-nat.c ppcfbsd-tdep.c \ ppcnbsd-nat.c ppcnbsd-tdep.c \ @@ -2228,6 +2228,14 @@ common-exceptions.o: ${srcdir}/common/common-exceptions.c $(COMPILE) $(srcdir)/common/common-exceptions.c $(POSTCOMPILE) +posix-strerror.o: ${srcdir}/common/posix-strerror.c + $(COMPILE) $(srcdir)/common/posix-strerror.c + $(POSTCOMPILE) + +mingw-strerror.o: ${srcdir}/common/mingw-strerror.c + $(COMPILE) $(srcdir)/common/mingw-strerror.c + $(POSTCOMPILE) + # # gdb/target/ dependencies # diff --git a/gdb/common/common-utils.h b/gdb/common/common-utils.h index f110924..60aa030 100644 --- a/gdb/common/common-utils.h +++ b/gdb/common/common-utils.h @@ -62,4 +62,10 @@ int xsnprintf (char *str, size_t size, const char *format, ...) char *savestring (const char *ptr, size_t len); +/* The strerror() function can return NULL for errno values that are + out of range. Provide a "safe" version that always returns a + printable string. */ + +extern char *safe_strerror (int); + #endif diff --git a/gdb/common/common.host b/gdb/common/common.host new file mode 100644 index 0000000..74c4515 --- /dev/null +++ b/gdb/common/common.host @@ -0,0 +1,17 @@ +# Mapping of configurations into GDB host definitions. This is +# invoked from the autoconf generated configure script. + +# This file sets the following shell variables: +# gdb_host_obs host-specific .o files to include when building GDB +# srv_host_obs likewise, but when building gdbserver + +case "${host}" in + +i[34567]86-*-mingw32*) gdb_host_obs="mingw-hdep.o mingw-strerror.o" + srv_host_obs="mingw-strerror.o" + ;; +x86_64-*-mingw*) gdb_host_obs="mingw-hdep.o mingw-strerror.o" + srv_host_obs="mingw-strerror.o" + ;; + +esac diff --git a/gdb/common/mingw-strerror.c b/gdb/common/mingw-strerror.c new file mode 100644 index 0000000..c62c42d --- /dev/null +++ b/gdb/common/mingw-strerror.c @@ -0,0 +1,64 @@ +/* Safe version of strerror for MinGW, for GDB, the GNU debugger. + + Copyright (C) 2006-2015 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include "common-defs.h" + +#include + +/* Implementation of safe_strerror as defined in common-utils.h. + + The Windows runtime implementation of strerror never returns NULL, + but does return a useless string for anything above sys_nerr; + unfortunately this includes all socket-related error codes. + This replacement tries to find a system-provided error message. */ + +char * +safe_strerror (int errnum) +{ + static char *buffer; + int len; + + if (errnum >= 0 && errnum < sys_nerr) + return strerror (errnum); + + if (buffer) + { + LocalFree (buffer); + buffer = NULL; + } + + if (FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER + | FORMAT_MESSAGE_FROM_SYSTEM, + NULL, errnum, + MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPTSTR) &buffer, 0, NULL) == 0) + { + static char buf[32]; + xsnprintf (buf, sizeof buf, "(undocumented errno %d)", errnum); + return buf; + } + + /* Windows error messages end with a period and a CR-LF; strip that + out. */ + len = strlen (buffer); + if (len > 3 && strcmp (buffer + len - 3, ".\r\n") == 0) + buffer[len - 3] = '\0'; + + return buffer; +} diff --git a/gdb/common/posix-strerror.c b/gdb/common/posix-strerror.c new file mode 100644 index 0000000..119fcf6 --- /dev/null +++ b/gdb/common/posix-strerror.c @@ -0,0 +1,38 @@ +/* Safe version of strerror for POSIX systems for GDB, the GNU debugger. + + Copyright (C) 2006-2015 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include "common-defs.h" + +/* Implementation of safe_strerror as defined in common-utils.h. */ + +char * +safe_strerror (int errnum) +{ + char *msg; + + msg = strerror (errnum); + if (msg == NULL) + { + static char buf[32]; + + xsnprintf (buf, sizeof buf, "(undocumented errno %d)", errnum); + msg = buf; + } + return (msg); +} diff --git a/gdb/configure b/gdb/configure index 7ff74ba..2d53196 100755 --- a/gdb/configure +++ b/gdb/configure @@ -5298,7 +5298,7 @@ fi # Provide defaults for some variables set by the per-host and per-target # configuration. -gdb_host_obs=posix-hdep.o +gdb_host_obs="posix-hdep.o posix-strerror.o" if test "${target}" = "${host}"; then gdb_native=yes @@ -5307,6 +5307,7 @@ else fi . $srcdir/configure.host +. $srcdir/common/common.host # Accumulate some settings from configure.tgt over all enabled targets diff --git a/gdb/configure.ac b/gdb/configure.ac index ec776d7..e03c747 100644 --- a/gdb/configure.ac +++ b/gdb/configure.ac @@ -195,7 +195,7 @@ esac],[want64=false])dnl # Provide defaults for some variables set by the per-host and per-target # configuration. -gdb_host_obs=posix-hdep.o +gdb_host_obs="posix-hdep.o posix-strerror.o" if test "${target}" = "${host}"; then gdb_native=yes @@ -204,6 +204,7 @@ else fi . $srcdir/configure.host +. $srcdir/common/common.host # Accumulate some settings from configure.tgt over all enabled targets diff --git a/gdb/configure.host b/gdb/configure.host index d07be4b..9f8a917 100644 --- a/gdb/configure.host +++ b/gdb/configure.host @@ -7,7 +7,6 @@ # gdb_host_float_format host's float floatformat, or 0 # gdb_host_double_format host's double floatformat, or 0 # gdb_host_long_double_format host's long double floatformat, or 0 -# gdb_host_obs host-specific .o files to include # Map host cpu into the config cpu subdirectory name. # The default is $host_cpu. @@ -105,7 +104,6 @@ i[34567]86-*-netbsdelf* | i[34567]86-*-knetbsd*-gnu) gdb_host=nbsdelf ;; i[34567]86-*-go32*) gdb_host=go32 ;; i[34567]86-*-mingw32*) gdb_host=mingw - gdb_host_obs=mingw-hdep.o ;; i[34567]86-*-msdosdjgpp*) gdb_host=go32 ;; i[34567]86-*-linux*) gdb_host=linux ;; @@ -181,7 +179,6 @@ x86_64-*-netbsd* | x86_64-*-knetbsd*-gnu) gdb_host=nbsd64 ;; x86_64-*-openbsd*) gdb_host=obsd64 ;; x86_64-*-mingw*) gdb_host=mingw64 - gdb_host_obs=mingw-hdep.o ;; x86_64-*-cygwin*) gdb_host=cygwin64 ;; m32r*-*-linux*) gdb_host=linux ;; diff --git a/gdb/contrib/ari/gdb_ari.sh b/gdb/contrib/ari/gdb_ari.sh index 8a8cec8..b868a17 100644 --- a/gdb/contrib/ari/gdb_ari.sh +++ b/gdb/contrib/ari/gdb_ari.sh @@ -603,8 +603,8 @@ BEGIN { doc["strerror"] = "\ Do not use strerror(), instead use safe_strerror()" category["strerror"] = ari_regression fix("strerror", "gdb/gdb_string.h", 1) - fix("strerror", "gdb/mingw-hdep.c", 1) - fix("strerror", "gdb/posix-hdep.c", 1) + fix("strerror", "gdb/common/mingw-strerror.c", 1) + fix("strerror", "gdb/common/posix-strerror.c", 1) } /(^|[^_[:alnum:]])strerror[[:space:]]*\(/ { fail("strerror") diff --git a/gdb/gdbserver/Makefile.in b/gdb/gdbserver/Makefile.in index 1ed2ec8..f05f450 100644 --- a/gdb/gdbserver/Makefile.in +++ b/gdb/gdbserver/Makefile.in @@ -518,6 +518,12 @@ rsp-low.o: ../common/rsp-low.c common-utils.o: ../common/common-utils.c $(COMPILE) $< $(POSTCOMPILE) +posix-strerror.o: ../common/posix-strerror.c + $(COMPILE) $< + $(POSTCOMPILE) +mingw-strerror.o: ../common/mingw-strerror.c + $(COMPILE) $< + $(POSTCOMPILE) vec.o: ../common/vec.c $(COMPILE) $< $(POSTCOMPILE) diff --git a/gdb/gdbserver/configure b/gdb/gdbserver/configure index 45efc51..cddc8d7 100755 --- a/gdb/gdbserver/configure +++ b/gdb/gdbserver/configure @@ -5537,7 +5537,12 @@ $as_echo "$gdb_cv_m68k_is_coldfire" >&6; } ;; esac +# Initialize as POSIX. This will change if the host is MingW. + +srv_host_obs="posix-strerror.o" + . ${srcdir}/configure.srv +. ${srcdir}/../common/common.host if test "${srv_mingwce}" = "yes"; then LIBS="$LIBS -lws2" @@ -6034,7 +6039,7 @@ $as_echo "#define USE_XML 1" >>confdefs.h done fi -GDBSERVER_DEPFILES="$srv_regobj $srv_tgtobj $srv_hostio_err_objs $srv_thread_depfiles" +GDBSERVER_DEPFILES="$srv_regobj $srv_tgtobj $srv_hostio_err_objs $srv_thread_depfiles $srv_host_obs" GDBSERVER_LIBS="$srv_libs" { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether the target supports __sync_*_compare_and_swap" >&5 diff --git a/gdb/gdbserver/configure.ac b/gdb/gdbserver/configure.ac index 02082cc..31fa42d 100644 --- a/gdb/gdbserver/configure.ac +++ b/gdb/gdbserver/configure.ac @@ -240,7 +240,12 @@ got it ;; esac +# Initialize as POSIX. This will change if the host is MingW. + +srv_host_obs="posix-strerror.o" + . ${srcdir}/configure.srv +. ${srcdir}/../common/common.host if test "${srv_mingwce}" = "yes"; then LIBS="$LIBS -lws2" @@ -385,7 +390,7 @@ if test "$srv_xmlfiles" != ""; then done fi -GDBSERVER_DEPFILES="$srv_regobj $srv_tgtobj $srv_hostio_err_objs $srv_thread_depfiles" +GDBSERVER_DEPFILES="$srv_regobj $srv_tgtobj $srv_hostio_err_objs $srv_thread_depfiles $srv_host_obs" GDBSERVER_LIBS="$srv_libs" dnl Check whether the target supports __sync_*_compare_and_swap. diff --git a/gdb/mingw-hdep.c b/gdb/mingw-hdep.c index c0cc385..a0ed281 100644 --- a/gdb/mingw-hdep.c +++ b/gdb/mingw-hdep.c @@ -35,50 +35,6 @@ static HANDLE sigint_event; function. */ struct async_signal_handler *sigint_handler; -/* The strerror() function can return NULL for errno values that are - out of range. Provide a "safe" version that always returns a - printable string. - - The Windows runtime implementation of strerror never returns NULL, - but does return a useless string for anything above sys_nerr; - unfortunately this includes all socket-related error codes. - This replacement tries to find a system-provided error message. */ - -char * -safe_strerror (int errnum) -{ - static char *buffer; - int len; - - if (errnum >= 0 && errnum < sys_nerr) - return strerror (errnum); - - if (buffer) - { - LocalFree (buffer); - buffer = NULL; - } - - if (FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER - | FORMAT_MESSAGE_FROM_SYSTEM, - NULL, errnum, - MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPTSTR) &buffer, 0, NULL) == 0) - { - static char buf[32]; - xsnprintf (buf, sizeof buf, "(undocumented errno %d)", errnum); - return buf; - } - - /* Windows error messages end with a period and a CR-LF; strip that - out. */ - len = strlen (buffer); - if (len > 3 && strcmp (buffer + len - 3, ".\r\n") == 0) - buffer[len - 3] = '\0'; - - return buffer; -} - /* Return an absolute file name of the running GDB, if possible, or ARGV0 if not. The return value is in malloc'ed storage. */ diff --git a/gdb/posix-hdep.c b/gdb/posix-hdep.c index 13cddba..fef5ec1 100644 --- a/gdb/posix-hdep.c +++ b/gdb/posix-hdep.c @@ -22,26 +22,6 @@ #include "gdb_select.h" -/* The strerror() function can return NULL for errno values that are - out of range. Provide a "safe" version that always returns a - printable string. */ - -char * -safe_strerror (int errnum) -{ - char *msg; - - msg = strerror (errnum); - if (msg == NULL) - { - static char buf[32]; - - xsnprintf (buf, sizeof buf, "(undocumented errno %d)", errnum); - msg = buf; - } - return (msg); -} - /* Wrapper for select. Nothing special needed on POSIX platforms. */ int diff --git a/gdb/utils.h b/gdb/utils.h index 3debde7..e58260c 100644 --- a/gdb/utils.h +++ b/gdb/utils.h @@ -42,9 +42,6 @@ ULONGEST strtoulst (const char *num, const char **trailer, int base); int compare_positive_ints (const void *ap, const void *bp); int compare_strings (const void *ap, const void *bp); -/* This is defined in *-hdep.c, e.g., posix-hdep.c. */ -extern char *safe_strerror (int); - /* A wrapper for bfd_errmsg to produce a more helpful error message in the case of bfd_error_file_ambiguously recognized. MATCHING, if non-NULL, is the corresponding argument to