[review] Use ctime_r and localtime_r if available

Christian Biesinger (Code Review)
2019-10-31T21:06:27Z

Use ctime_r and localtime_r if available

This requires definining _POSIX_C_SOURCE for glibc. I don't know if there
are any undesired side-effects from that.

I am also adding the strerror_r check to gdbserver's configure.ac
that I previously forgot (but gdbserver doesn't use threads as
much, so that's less critical)

gdb/ChangeLog:

2019-10-31 Christian Biesinger <cbiesinger@google.com>

* config.in: Regenerate.
* configure: Regenerate.
* configure.ac: Check for localtime_r and ctime_r, and define
_POSIX_C_SOURCE so that we actually find them.
* maint.c (scoped_command_stats::print_time): Use localtime_r
when available instead of localtime.
* nat/linux-osdata.c (time_from_time_t): Use ctime_r if available
instead of ctime.

gdb/gdbserver/ChangeLog:

2019-10-31 Christian Biesinger <cbiesinger@google.com>

* config.in: Regenerate.
* configure: Regenerate.
* configure.ac: Define _POSIX_C_SOURCE and search for strerror_r
and ctime_r.

Change-Id: I329bbdc39d5b576f51859ba00f1617e024c30cbd
---
M gdb/config.in
M gdb/configure
M gdb/configure.ac
M gdb/gdbserver/config.in
M gdb/gdbserver/configure
M gdb/gdbserver/configure.ac
M gdb/maint.c
M gdb/nat/linux-osdata.c
8 files changed, 42 insertions(+), 5 deletions(-)

diff --git a/gdb/config.in b/gdb/config.in
index 5a21fca..a676b47 100644
--- a/gdb/config.in
+++ b/gdb/config.in
@@ -78,6 +78,9 @@
/* Define to 1 if you have the `btowc' function. */
#undef HAVE_BTOWC

+/* Define to 1 if you have the `ctime_r' function. */
+#undef HAVE_CTIME_R
+
/* Define to 1 if you have the <cursesX.h> header file. */
#undef HAVE_CURSESX_H

@@ -261,6 +264,9 @@
/* Define to 1 if you have the <locale.h> header file. */
#undef HAVE_LOCALE_H

+/* Define to 1 if you have the `localtime_r' function. */
+#undef HAVE_LOCALTIME_R
+
/* Define to 1 if the compiler supports long double. */
#undef HAVE_LONG_DOUBLE

@@ -780,6 +786,9 @@
this defined. */
#undef _POSIX_1_SOURCE

+/* We want the latest POSIX standard */
+#undef _POSIX_C_SOURCE
+
/* Define to 1 if you need to in order for `stat' and other things to work. */
#undef _POSIX_SOURCE

diff --git a/gdb/configure b/gdb/configure
index 018cc4b..42cfb5b 100755
--- a/gdb/configure
+++ b/gdb/configure
@@ -4408,6 +4408,9 @@
$as_echo "#define _TANDEM_SOURCE 1" >>confdefs.h

+
+$as_echo "#define _POSIX_C_SOURCE 200809L" >>confdefs.h
+
ac_aux_dir=
for ac_dir in "$srcdir" "$srcdir/.." "$srcdir/../.."; do
if test -f "$ac_dir/install-sh"; then
@@ -13072,7 +13075,7 @@
sbrk getpgid setpgid setpgrp setsid \
sigaction sigsetmask socketpair \
ttrace wborder wresize setlocale iconvlist libiconvlist btowc \
- setrlimit getrlimit posix_madvise waitpid \
+ setrlimit getrlimit posix_madvise waitpid ctime_r localtime_r \
ptrace64 sigaltstack setns use_default_colors strerror_r
do :
as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
diff --git a/gdb/configure.ac b/gdb/configure.ac
index 987507a..6a6ec5b 100644
--- a/gdb/configure.ac
+++ b/gdb/configure.ac
@@ -29,6 +29,7 @@
AC_PROG_CXX

AC_USE_SYSTEM_EXTENSIONS
+AC_DEFINE([_POSIX_C_SOURCE], [200809L], [We want the latest POSIX standard])
ACX_LARGEFILE
AM_PROG_CC_STDC
AM_PROG_INSTALL_STRIP
@@ -1317,7 +1318,7 @@
sbrk getpgid setpgid setpgrp setsid \
sigaction sigsetmask socketpair \
ttrace wborder wresize setlocale iconvlist libiconvlist btowc \
- setrlimit getrlimit posix_madvise waitpid \
+ setrlimit getrlimit posix_madvise waitpid ctime_r localtime_r \
ptrace64 sigaltstack setns use_default_colors strerror_r])
AM_LANGINFO_CODESET
GDB_AC_COMMON
diff --git a/gdb/gdbserver/config.in b/gdb/gdbserver/config.in
index 0bce18d..78fbf26 100644
--- a/gdb/gdbserver/config.in
+++ b/gdb/gdbserver/config.in
@@ -21,6 +21,9 @@
/* Define to 1 if you have the <arpa/inet.h> header file. */
#undef HAVE_ARPA_INET_H

+/* Define to 1 if you have the `ctime_r' function. */
+#undef HAVE_CTIME_R
+
/* define if the compiler supports basic C++11 syntax */
#undef HAVE_CXX11

@@ -229,6 +232,9 @@
/* Define to 1 if you have the <stdlib.h> header file. */
#undef HAVE_STDLIB_H

+/* Define to 1 if you have the `strerror_r' function. */
+#undef HAVE_STRERROR_R
+
/* Define to 1 if you have the <strings.h> header file. */
#undef HAVE_STRINGS_H

@@ -420,6 +426,9 @@
this defined. */
#undef _POSIX_1_SOURCE

+/* We want the latest POSIX standard */
+#undef _POSIX_C_SOURCE
+
/* Define to 1 if you need to in order for `stat' and other things to work. */
#undef _POSIX_SOURCE

diff --git a/gdb/gdbserver/configure b/gdb/gdbserver/configure
index e513fc5..8dd13b0 100755
--- a/gdb/gdbserver/configure
+++ b/gdb/gdbserver/configure
@@ -4094,6 +4094,9 @@

+
+$as_echo "#define _POSIX_C_SOURCE 200809L" >>confdefs.h
+
# Check whether --enable-largefile was given.
if test "${enable_largefile+set}" = set; then :
enableval=$enable_largefile;
@@ -6448,7 +6451,7 @@

fi

-for ac_func in getauxval pread pwrite pread64 setns
+for ac_func in getauxval pread pwrite pread64 setns strerror_r ctime_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/gdbserver/configure.ac b/gdb/gdbserver/configure.ac
index 7ebc9c3..972a17d 100644
--- a/gdb/gdbserver/configure.ac
+++ b/gdb/gdbserver/configure.ac
@@ -26,6 +26,7 @@
AC_PROG_CC
AC_PROG_CXX
AC_GNU_SOURCE
+AC_DEFINE([_POSIX_C_SOURCE], [200809L], [We want the latest POSIX standard])
AC_SYS_LARGEFILE

AC_CANONICAL_SYSTEM
@@ -90,7 +91,7 @@
sys/ioctl.h netinet/in.h sys/socket.h netdb.h dnl
netinet/tcp.h arpa/inet.h)
AC_FUNC_FORK
-AC_CHECK_FUNCS(getauxval pread pwrite pread64 setns)
+AC_CHECK_FUNCS(getauxval pread pwrite pread64 setns strerror_r ctime_r)

GDB_AC_COMMON

diff --git a/gdb/maint.c b/gdb/maint.c
index ec9f4ab..36ab716 100644
--- a/gdb/maint.c
+++ b/gdb/maint.c
@@ -1039,7 +1039,12 @@
auto millis = ticks % 1000;

std::time_t as_time = system_clock::to_time_t (now);
+#ifdef HAVE_LOCALTIME_R
+ struct tm buf;
+ struct tm *tm = localtime_r (&as_time, &buf);
+#else
struct tm *tm = localtime (&as_time);
+#endif

char out[100];
strftime (out, sizeof (out), "%F %H:%M:%S", tm);
diff --git a/gdb/nat/linux-osdata.c b/gdb/nat/linux-osdata.c
index 67f9f3a..e0bad81 100644
--- a/gdb/nat/linux-osdata.c
+++ b/gdb/nat/linux-osdata.c
@@ -912,7 +912,13 @@
{
time_t t = (time_t) seconds;

- strncpy (time, ctime (&t), maxlen);
+ char buf[30];
+#ifdef HAVE_CTIME_R
+ const char *time_str = ctime_r (&t, buf);
+#else
+ const char *time_str = ctime (&t);
+#endif
+ strncpy (time, time_str, maxlen);
time[maxlen - 1] = '\0';
}
}

--
Gerrit-Project: binutils-gdb
Gerrit-Branch: master
Gerrit-Change-Id: I329bbdc39d5b576f51859ba00f1617e024c30cbd
Gerrit-Change-Number: 475
Gerrit-PatchSet: 1
Gerrit-Owner: Christian Biesinger <cbiesinger@google.com>
Gerrit-MessageType: newchange