diff options
author | Christian Biesinger <cbiesinger@google.com> | 2019-10-31 14:41:19 -0500 |
---|---|---|
committer | Christian Biesinger <cbiesinger@google.com> | 2019-10-31 15:14:14 -0500 |
commit | b231e86ac9608056ea837e24d42a878927f5787a (patch) | |
tree | 29b98002638e43c04d5a02a23501ea717932c750 /gdb/gdbsupport | |
parent | a2726d4ff80168a8134c68cb798e3f5f537b0eba (diff) | |
download | gdb-b231e86ac9608056ea837e24d42a878927f5787a.zip gdb-b231e86ac9608056ea837e24d42a878927f5787a.tar.gz gdb-b231e86ac9608056ea837e24d42a878927f5787a.tar.bz2 |
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 <cbiesinger@google.com>
* 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
Diffstat (limited to 'gdb/gdbsupport')
-rw-r--r-- | gdb/gdbsupport/common-utils.h | 4 | ||||
-rw-r--r-- | gdb/gdbsupport/posix-strerror.c | 20 |
2 files changed, 17 insertions, 7 deletions
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 @@ std::string extract_string_maybe_quoted (const char **arg); /* 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; |