PATCH: Introduce gdb_gettimeofday

Mark Mitchell
2005-03-08T23:29:44Z

Some systems (Windows) don't have gettimeofday. This patch introduces
a new utility function gdb_gettimeofday; on UNIX systems this is just
a wrapper for gettimeofday, while on other systems it relies on
fallback methods. (In particular, on Windows, we use "time", at least
for now.)

Tested by building on both Windows (with other patches not yet
submitted) and on x86_64-uknown-linux-gnu, and by running the
testsuite on the latter platform with no regressions. I'll check for
copyrights before submission, if this is approved.

OK?

--
Mark Mitchell
CodeSourcery, LLC
mark@codesourcery.com

2005-03-08 Mark Mitchell <mark@codesourcery.com>

* config.in (HAVE_GETTIMEOFDAY): #undef it.
* configure.ac (gettimeofday): Call AC_CHECK_FUNCS for it.
* configure: Regenerated.
* defs.h (gdb_gettimeofday): Declare.
* event-loop.c (create_timer): Use it, instead of gettimeofday.
(handle_timer_event): Likewise.
(poll_timers): Likewise.
* remote-fileio.c (remote_fileio_func_fstat): Likewise.
(remote_fileio_func_gettimeofday): Likewise.
* utils.c (<sys/time.h>): Include it.
(gdb_gettimeofday): New function.
* mi/mi-main.c (mi_load_progress): Use gdb_gettimeofday, instead
of gettimeofday.

Index: config.in
===================================================================
RCS file: /cvs/src/src/gdb/config.in,v
retrieving revision 1.76
diff -c -5 -p -r1.76 config.in
*** config.in 21 Jan 2005 13:49:22 -0000 1.76
--- config.in 8 Mar 2005 23:20:48 -0000
***************
*** 251,260 ****
--- 251,263 ----
#undef HAVE_GETPAGESIZE

/* Define as 1 if you have gettext and don't want to use GNU gettext. */
#undef HAVE_GETTEXT

+ /* Define to 1 if you have the `gettimeofday' function. */
+ #undef HAVE_GETTIMEOFDAY
+
/* Define to 1 if you have the <gnu/libc-version.h> header file. */
#undef HAVE_GNU_LIBC_VERSION_H

/* Define if <sys/procfs.h> has gregset_t. */
#undef HAVE_GREGSET_T
Index: configure.ac
===================================================================
RCS file: /cvs/src/src/gdb/configure.ac,v
retrieving revision 1.12
diff -c -5 -p -r1.12 configure.ac
*** configure.ac 22 Feb 2005 23:25:11 -0000 1.12
--- configure.ac 8 Mar 2005 23:20:49 -0000
*************** AC_CHECK_FUNCS(setpgid setpgrp)
*** 454,463 ****
--- 454,464 ----
AC_CHECK_FUNCS(sigaction sigprocmask sigsetmask)
AC_CHECK_FUNCS(socketpair)
AC_CHECK_FUNCS(syscall)
AC_CHECK_FUNCS(ttrace)
AC_CHECK_FUNCS(wborder)
+ AC_CHECK_FUNCS(gettimeofday)

# Check the return and argument types of ptrace. No canned test for
# this, so roll our own.
gdb_ptrace_headers='
#if HAVE_SYS_TYPES_H
Index: defs.h
===================================================================
RCS file: /cvs/src/src/gdb/defs.h,v
retrieving revision 1.181
diff -c -5 -p -r1.181 defs.h
*** defs.h 14 Feb 2005 14:37:37 -0000 1.181
--- defs.h 8 Mar 2005 23:20:49 -0000
*************** extern int use_windows;
*** 1206,1211 ****
--- 1206,1218 ----
this incorrect coding style. */

extern ULONGEST align_up (ULONGEST v, int n);
extern ULONGEST align_down (ULONGEST v, int n);

+ /* Forward declaration so that <sys/time.h> does not to be included
+ here. */
+ struct timeval;
+ /* Like gettimeofday, but always returns the local time, and uses
+ whatever time functionality is available on the system. */
+ extern int gdb_gettimeofday (struct timeval *tp);
+
#endif /* #ifndef DEFS_H */
Index: event-loop.c
===================================================================
RCS file: /cvs/src/src/gdb/event-loop.c,v
retrieving revision 1.24
diff -c -5 -p -r1.24 event-loop.c
*** event-loop.c 12 Feb 2005 00:39:18 -0000 1.24
--- event-loop.c 8 Mar 2005 23:20:49 -0000
*************** create_timer (int milliseconds, timer_ha
*** 973,983 ****
/* compute seconds */
delta.tv_sec = milliseconds / 1000;
/* compute microseconds */
delta.tv_usec = (milliseconds % 1000) * 1000;

! gettimeofday (&time_now, NULL);

timer_ptr = (struct gdb_timer *) xmalloc (sizeof (gdb_timer));
timer_ptr->when.tv_sec = time_now.tv_sec + delta.tv_sec;
timer_ptr->when.tv_usec = time_now.tv_usec + delta.tv_usec;
/* carry? */
--- 973,983 ----
/* compute seconds */
delta.tv_sec = milliseconds / 1000;
/* compute microseconds */
delta.tv_usec = (milliseconds % 1000) * 1000;

! gdb_gettimeofday (&time_now);

timer_ptr = (struct gdb_timer *) xmalloc (sizeof (gdb_timer));
timer_ptr->when.tv_sec = time_now.tv_sec + delta.tv_sec;
timer_ptr->when.tv_usec = time_now.tv_usec + delta.tv_usec;
/* carry? */
*************** static void
*** 1069,1079 ****
handle_timer_event (int dummy)
{
struct timeval time_now;
struct gdb_timer *timer_ptr, *saved_timer;

! gettimeofday (&time_now, NULL);
timer_ptr = timer_list.first_timer;

while (timer_ptr != NULL)
{
if ((timer_ptr->when.tv_sec > time_now.tv_sec) ||
--- 1069,1079 ----
handle_timer_event (int dummy)
{
struct timeval time_now;
struct gdb_timer *timer_ptr, *saved_timer;

! gdb_gettimeofday (&time_now);
timer_ptr = timer_list.first_timer;

while (timer_ptr != NULL)
{
if ((timer_ptr->when.tv_sec > time_now.tv_sec) ||
*************** poll_timers (void)
*** 1105,1115 ****
struct timeval time_now, delta;
gdb_event *event_ptr;

if (timer_list.first_timer != NULL)
{
! gettimeofday (&time_now, NULL);
delta.tv_sec = timer_list.first_timer->when.tv_sec - time_now.tv_sec;
delta.tv_usec = timer_list.first_timer->when.tv_usec - time_now.tv_usec;
/* borrow? */
if (delta.tv_usec < 0)
{
--- 1105,1115 ----
struct timeval time_now, delta;
gdb_event *event_ptr;

if (timer_list.first_timer != NULL)
{
! gdb_gettimeofday (&time_now);
delta.tv_sec = timer_list.first_timer->when.tv_sec - time_now.tv_sec;
delta.tv_usec = timer_list.first_timer->when.tv_usec - time_now.tv_usec;
/* borrow? */
if (delta.tv_usec < 0)
{
Index: remote-fileio.c
===================================================================
RCS file: /cvs/src/src/gdb/remote-fileio.c,v
retrieving revision 1.12
diff -c -5 -p -r1.12 remote-fileio.c
*** remote-fileio.c 14 Feb 2005 18:10:10 -0000 1.12
--- remote-fileio.c 8 Mar 2005 23:20:49 -0000
*************** remote_fileio_func_fstat (char *buf)
*** 1136,1146 ****
st.st_size = 0;
st.st_blksize = 512;
#if HAVE_STRUCT_STAT_ST_BLOCKS
st.st_blocks = 0;
#endif
! if (!gettimeofday (&tv, NULL))
st.st_atime = st.st_mtime = st.st_ctime = tv.tv_sec;
else
st.st_atime = st.st_mtime = st.st_ctime = (time_t) 0;
ret = 0;
}
--- 1136,1146 ----
st.st_size = 0;
st.st_blksize = 512;
#if HAVE_STRUCT_STAT_ST_BLOCKS
st.st_blocks = 0;
#endif
! if (!gdb_gettimeofday (&tv))
st.st_atime = st.st_mtime = st.st_ctime = tv.tv_sec;
else
st.st_atime = st.st_mtime = st.st_ctime = (time_t) 0;
ret = 0;
}
*************** remote_fileio_func_gettimeofday (char *b
*** 1194,1204 ****
remote_fileio_reply (-1, FILEIO_EINVAL);
return;
}

remote_fio_no_longjmp = 1;
! ret = gettimeofday (&tv, NULL);

if (ret == -1)
{
remote_fileio_return_errno (-1);
return;
--- 1194,1204 ----
remote_fileio_reply (-1, FILEIO_EINVAL);
return;
}

remote_fio_no_longjmp = 1;
! ret = gdb_gettimeofday (&tv);

if (ret == -1)
{
remote_fileio_return_errno (-1);
return;
Index: utils.c
===================================================================
RCS file: /cvs/src/src/gdb/utils.c,v
retrieving revision 1.157
diff -c -5 -p -r1.157 utils.c
*** utils.c 24 Feb 2005 13:51:35 -0000 1.157
--- utils.c 8 Mar 2005 23:20:50 -0000
***************
*** 55,64 ****
--- 55,65 ----
#include "symfile.h"

#include "inferior.h" /* for signed_pointer_to_address */

#include <sys/param.h> /* For MAXPATHLEN */
+ #include <sys/time.h>

#include "gdb_curses.h"

#include "readline/readline.h"

*************** align_down (ULONGEST v, int n)
*** 3094,3098 ****
--- 3095,3116 ----
{
/* Check that N is really a power of two. */
gdb_assert (n && (n & (n-1)) == 0);
return (v & -n);
}
+
+ /* A wrapper around gettimeofday. On systems that do not have
+ gettimeofday, use other methods to get the current time. */
+ int
+ gdb_gettimeofday (struct timeval *tp)
+ {
+ #ifdef HAVE_GETTIMEOFDAY
+ return gettimeofday (tp, NULL);
+ #else
+ /* On systems without gettimeofday, use the low-resolution "time"
+ function. */
+ time_t system_time;
+ time (&tp->tv_sec);
+ tp->tv_usec = 0;
+ return 0;
+ #endif
+ }
Index: mi/mi-main.c
===================================================================
RCS file: /cvs/src/src/gdb/mi/mi-main.c,v
retrieving revision 1.76
diff -c -5 -p -r1.76 mi-main.c
*** mi/mi-main.c 21 Feb 2005 03:59:23 -0000 1.76
--- mi/mi-main.c 8 Mar 2005 23:20:50 -0000
*************** mi_load_progress (const char *section_na
*** 1375,1385 ****
&& !current_interp_named_p (INTERP_MI1))
return;

update_threshold.tv_sec = 0;
update_threshold.tv_usec = 500000;
! gettimeofday (&time_now, NULL);

delta.tv_usec = time_now.tv_usec - last_update.tv_usec;
delta.tv_sec = time_now.tv_sec - last_update.tv_sec;

if (delta.tv_usec < 0)
--- 1375,1385 ----
&& !current_interp_named_p (INTERP_MI1))
return;

update_threshold.tv_sec = 0;
update_threshold.tv_usec = 500000;
! gdb_gettimeofday (&time_now);

delta.tv_usec = time_now.tv_usec - last_update.tv_usec;
delta.tv_sec = time_now.tv_sec - last_update.tv_sec;

if (delta.tv_usec < 0)