From patchwork Thu Oct 31 20:15:06 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Simon Marchi (Code Review)" X-Patchwork-Id: 35535 Received: (qmail 114497 invoked by alias); 31 Oct 2019 20:15:44 -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 114352 invoked by uid 89); 31 Oct 2019 20:15:33 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3 autolearn=ham version=3.3.1 spammy= X-HELO: mx1.osci.io Received: from polly.osci.io (HELO mx1.osci.io) (8.43.85.229) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 31 Oct 2019 20:15:19 +0000 Received: by mx1.osci.io (Postfix, from userid 994) id 46FC920468; Thu, 31 Oct 2019 16:15:13 -0400 (EDT) Received: from gnutoolchain-gerrit.osci.io (gnutoolchain-gerrit.osci.io [IPv6:2620:52:3:1:5054:ff:fe06:16ca]) by mx1.osci.io (Postfix) with ESMTP id 09F1D2031F; Thu, 31 Oct 2019 16:15:07 -0400 (EDT) Received: from localhost (localhost [127.0.0.1]) by gnutoolchain-gerrit.osci.io (Postfix) with ESMTP id E3C7020AF6; Thu, 31 Oct 2019 16:15:06 -0400 (EDT) X-Gerrit-PatchSet: 3 Date: Thu, 31 Oct 2019 16:15:06 -0400 From: "Sourceware to Gerrit sync (Code Review)" To: Christian Biesinger , Simon Marchi , gdb-patches@sourceware.org Auto-Submitted: auto-generated X-Gerrit-MessageType: newpatchset Subject: [pushed] Use strerror_r in safe_strerror if available X-Gerrit-Change-Id: I81048fbaf148035c221c528727f7efe58ba528eb X-Gerrit-Change-Number: 474 X-Gerrit-ChangeURL: X-Gerrit-Commit: b231e86ac9608056ea837e24d42a878927f5787a In-Reply-To: References: Reply-To: noreply@gnutoolchain-gerrit.osci.io, simon.marchi@polymtl.ca, cbiesinger@google.com, gdb-patches@sourceware.org MIME-Version: 1.0 Content-Disposition: inline User-Agent: Gerrit/3.0.3-75-g9005159e5d Message-Id: <20191031201506.E3C7020AF6@gnutoolchain-gerrit.osci.io> The original change was created by Christian Biesinger. Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/474 ...................................................................... Use strerror_r in safe_strerror if available Also stores the result in a thread-local static variable and changes the return value to a const char*. This is already important because Guile creates threads and Python can create threads, but with the patch series here: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/176 GDB itself will create threads, too. gdb/ChangeLog: 2019-10-31 Christian Biesinger * configure: Regenerate. * configure.ac: Check for strerror_r. * gdbsupport/common-utils.h (safe_strerror): Change return value to const char * and document that this function is now threadsafe. * gdbsupport/posix-strerror.c (safe_strerror): Make buf thread_local and call strerror_r, if available. * utils.c (perror_string): Update. (print_sys_errmsg): Update. Change-Id: I81048fbaf148035c221c528727f7efe58ba528eb --- M gdb/ChangeLog M gdb/configure M gdb/configure.ac M gdb/gdbsupport/common-utils.h M gdb/gdbsupport/posix-strerror.c M gdb/utils.c 6 files changed, 32 insertions(+), 16 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 7abe20e..b7a7d98 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,14 @@ +2019-10-31 Christian Biesinger + + * configure: Regenerate. + * configure.ac: Check for strerror_r. + * gdbsupport/common-utils.h (safe_strerror): Change return value + to const char * and document that this function is now threadsafe. + * gdbsupport/posix-strerror.c (safe_strerror): Make buf + thread_local and call strerror_r, if available. + * utils.c (perror_string): Update. + (print_sys_errmsg): Update. + 2019-10-31 Luis Machado * arm-tdep.c (arm_exidx_data_key): Use bfd_key instead of diff --git a/gdb/configure b/gdb/configure index e805903..018cc4b 100755 --- a/gdb/configure +++ b/gdb/configure @@ -13073,7 +13073,7 @@ sigaction sigsetmask socketpair \ ttrace wborder wresize setlocale iconvlist libiconvlist btowc \ setrlimit getrlimit posix_madvise waitpid \ - ptrace64 sigaltstack setns use_default_colors + ptrace64 sigaltstack setns use_default_colors strerror_r do : as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh` ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var" diff --git a/gdb/configure.ac b/gdb/configure.ac index 354bb7b..987507a 100644 --- a/gdb/configure.ac +++ b/gdb/configure.ac @@ -1318,7 +1318,7 @@ sigaction sigsetmask socketpair \ ttrace wborder wresize setlocale iconvlist libiconvlist btowc \ setrlimit getrlimit posix_madvise waitpid \ - ptrace64 sigaltstack setns use_default_colors]) + ptrace64 sigaltstack setns use_default_colors strerror_r]) AM_LANGINFO_CODESET GDB_AC_COMMON diff --git a/gdb/gdbsupport/common-utils.h b/gdb/gdbsupport/common-utils.h index 23bf354..89868fc 100644 --- a/gdb/gdbsupport/common-utils.h +++ b/gdb/gdbsupport/common-utils.h @@ -108,9 +108,9 @@ /* The strerror() function can return NULL for errno values that are out of range. Provide a "safe" version that always returns a - printable string. */ + printable string. This version is also thread-safe. */ -extern char *safe_strerror (int); +extern const char *safe_strerror (int); /* Return true if the start of STRING matches PATTERN, false otherwise. */ diff --git a/gdb/gdbsupport/posix-strerror.c b/gdb/gdbsupport/posix-strerror.c index a8651b7..34420cf 100644 --- a/gdb/gdbsupport/posix-strerror.c +++ b/gdb/gdbsupport/posix-strerror.c @@ -21,15 +21,25 @@ /* Implementation of safe_strerror as defined in common-utils.h. */ -char * +const char * safe_strerror (int errnum) { - char *msg; + static thread_local char buf[1024]; + char *msg = nullptr; +#ifdef HAVE_STRERROR_R +# if !__GLIBC__ || ((_POSIX_C_SOURCE >= 200112L) && ! _GNU_SOURCE) + /* Glibc has two different, incompatible versions of strerror_r. */ + if (strerror_r (errnum, buf, sizeof (buf)) == 0) + msg = buf; +# else + msg = strerror_r (errnum, buf, sizeof (buf)); +# endif +#else msg = strerror (errnum); - if (msg == NULL) +#endif + if (msg == nullptr) { - static char buf[32]; xsnprintf (buf, sizeof buf, "(undocumented errno %d)", errnum); msg = buf; diff --git a/gdb/utils.c b/gdb/utils.c index 0c133d1..e06eedd 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -582,9 +582,7 @@ static std::string perror_string (const char *prefix) { - char *err; - - err = safe_strerror (errno); + const char *err = safe_strerror (errno); return std::string (prefix) + ": " + err; } @@ -630,11 +628,8 @@ void print_sys_errmsg (const char *string, int errcode) { - char *err; - char *combined; - - err = safe_strerror (errcode); - combined = (char *) alloca (strlen (err) + strlen (string) + 3); + const char *err = safe_strerror (errcode); + char *combined = (char *) alloca (strlen (err) + strlen (string) + 3); strcpy (combined, string); strcat (combined, ": "); strcat (combined, err);