From 064eee739c55428d8d297408e8e79abfe55935f7 Mon Sep 17 00:00:00 2001 From: Mark Mitchell Date: Mon, 28 Mar 2005 18:46:22 +0000 Subject: * gdb/configure.ac: Link with -lws2_32 on mingw. * gdb/configure: Regenerated. * gdb/defs.h (WINAPI): Define, conditionally. * gdb/ser-base.c (winsock2.h): Include it. (fd_event): Use read_prim. (ser_base_wait_for): Moved here from ser-unix.c. (do_ser_base_readchar): Likewise. (generic_readchar): Likewise. (ser_base_readchar): Likewise. * gdb/ser-base.h (generic_readchar): Declare. (ser_base_readchar): Likewise. * gdb/ser-pipe.c (_initialize_ser_pipe): Adjust for name changes. * gdb/ser-tcp.c (winsock2.h): Include it. (ETIMEDOUT): Define on Windows. (closesocket): Define on UNIX. (ioctlsocket): Likewise. (net_open): Adjust for differences in socket functions between Windows and UNIX. (_initialize_ser_tcp): Adjust for name changes. (net_read_prim): New function. (net_write_prim): Likewise. * gdb/ser-unix.c (generic_readchar): Remove. (ser_unix_wait_for): Likewise. (do_unix_readchar): Likewise. (ser_unix_readchar): Likewise. (_initialize_ser_hardwire): Adjust for name changes. (ser_unix_read_prim): New function. (ser_unix_write_prim): New function. * gdb/ser-unix.h (ser_unix_readchar): Remove. (ser_unix_read_prim): Declare. (ser_unix_write_prim): Declare. * gdb/serial.h (struct serial_ops): Add read_prim and write_prim. * gdb/utils.c (winerror.h): Include it. (safe_strerror): Handle winsock errors. --- gdb/configure | 110 +++++++++++++++++++++++++++ gdb/configure.ac | 8 ++ gdb/defs.h | 4 + gdb/ser-pipe.c | 4 +- gdb/ser-tcp.c | 75 ++++++++++++++++--- gdb/ser-unix.c | 221 ++++++++----------------------------------------------- gdb/ser-unix.h | 4 +- gdb/serial.h | 6 ++ gdb/utils.c | 26 ++++++- 9 files changed, 251 insertions(+), 207 deletions(-) (limited to 'gdb') diff --git a/gdb/configure b/gdb/configure index 39af594..fa00f77 100755 --- a/gdb/configure +++ b/gdb/configure @@ -15593,6 +15593,109 @@ fi done + +for ac_func in recv send +do +as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh` +echo "$as_me:$LINENO: checking for $ac_func" >&5 +echo $ECHO_N "checking for $ac_func... $ECHO_C" >&6 +if eval "test \"\${$as_ac_var+set}\" = set"; then + echo $ECHO_N "(cached) $ECHO_C" >&6 +else + cat >conftest.$ac_ext <<_ACEOF +/* confdefs.h. */ +_ACEOF +cat confdefs.h >>conftest.$ac_ext +cat >>conftest.$ac_ext <<_ACEOF +/* end confdefs.h. */ +/* Define $ac_func to an innocuous variant, in case declares $ac_func. + For example, HP-UX 11i declares gettimeofday. */ +#define $ac_func innocuous_$ac_func + +/* System header to define __stub macros and hopefully few prototypes, + which can conflict with char $ac_func (); below. + Prefer to if __STDC__ is defined, since + exists even on freestanding compilers. */ + +#ifdef __STDC__ +# include +#else +# include +#endif + +#undef $ac_func + +/* Override any gcc2 internal prototype to avoid an error. */ +#ifdef __cplusplus +extern "C" +{ +#endif +/* We use char because int might match the return type of a gcc2 + builtin and then its argument prototype would still apply. */ +char $ac_func (); +/* The GNU C library defines this for functions which it implements + to always fail with ENOSYS. Some functions are actually named + something starting with __ and the normal name is an alias. */ +#if defined (__stub_$ac_func) || defined (__stub___$ac_func) +choke me +#else +char (*f) () = $ac_func; +#endif +#ifdef __cplusplus +} +#endif + +int +main () +{ +return f != $ac_func; + ; + return 0; +} +_ACEOF +rm -f conftest.$ac_objext conftest$ac_exeext +if { (eval echo "$as_me:$LINENO: \"$ac_link\"") >&5 + (eval $ac_link) 2>conftest.er1 + ac_status=$? + grep -v '^ *+' conftest.er1 >conftest.err + rm -f conftest.er1 + cat conftest.err >&5 + echo "$as_me:$LINENO: \$? = $ac_status" >&5 + (exit $ac_status); } && + { ac_try='test -z "$ac_c_werror_flag" + || test ! -s conftest.err' + { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5 + (eval $ac_try) 2>&5 + ac_status=$? + echo "$as_me:$LINENO: \$? = $ac_status" >&5 + (exit $ac_status); }; } && + { ac_try='test -s conftest$ac_exeext' + { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5 + (eval $ac_try) 2>&5 + ac_status=$? + echo "$as_me:$LINENO: \$? = $ac_status" >&5 + (exit $ac_status); }; }; then + eval "$as_ac_var=yes" +else + echo "$as_me: failed program was:" >&5 +sed 's/^/| /' conftest.$ac_ext >&5 + +eval "$as_ac_var=no" +fi +rm -f conftest.err conftest.$ac_objext \ + conftest$ac_exeext conftest.$ac_ext +fi +echo "$as_me:$LINENO: result: `eval echo '${'$as_ac_var'}'`" >&5 +echo "${ECHO_T}`eval echo '${'$as_ac_var'}'`" >&6 +if test `eval echo '${'$as_ac_var'}'` = yes; then + cat >>confdefs.h <<_ACEOF +#define `echo "HAVE_$ac_func" | $as_tr_cpp` 1 +_ACEOF + +fi +done + + for ac_func in sbrk do as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh` @@ -20034,6 +20137,13 @@ if test x$gdb_cv_os_cygwin = xyes; then esac fi +# The ser-tcp.c module requires sockets. +case ${host} in + *mingw32*) + WIN32LIBS="$WIN32LIBS -lws2_32" + ;; +esac + LIBGUI="../libgui/src/libgui.a" GUI_CFLAGS_X="-I${srcdir}/../libgui/src" diff --git a/gdb/configure.ac b/gdb/configure.ac index 6113b0d..146f0e8 100644 --- a/gdb/configure.ac +++ b/gdb/configure.ac @@ -451,6 +451,7 @@ AC_CHECK_FUNCS(canonicalize_file_name realpath) AC_CHECK_FUNCS(getuid getgid) AC_CHECK_FUNCS(poll) AC_CHECK_FUNCS(pread64) +AC_CHECK_FUNCS(recv send) AC_CHECK_FUNCS(sbrk) AC_CHECK_FUNCS(setpgid setpgrp) AC_CHECK_FUNCS(sigaction sigprocmask sigsetmask) @@ -1187,6 +1188,13 @@ if test x$gdb_cv_os_cygwin = xyes; then ;; esac fi + +# The ser-tcp.c module requires sockets. +case ${host} in + *mingw32*) + WIN32LIBS="$WIN32LIBS -lws2_32" + ;; +esac AC_SUBST(WIN32LIBS) LIBGUI="../libgui/src/libgui.a" diff --git a/gdb/defs.h b/gdb/defs.h index 40ac441..e6384ab 100644 --- a/gdb/defs.h +++ b/gdb/defs.h @@ -1153,6 +1153,10 @@ extern int use_windows; #define SLASH_STRING "/" #endif +#if defined(__WIN32__) && !defined(__CYGWIN__) +#define WINAPI +#endif + /* Provide default definitions of PIDGET, TIDGET, and MERGEPID. The name ``TIDGET'' is a historical accident. Many uses of TIDGET in the code actually refer to a lightweight process id, i.e, diff --git a/gdb/ser-pipe.c b/gdb/ser-pipe.c index b6bb8d5..f127f5d 100644 --- a/gdb/ser-pipe.c +++ b/gdb/ser-pipe.c @@ -144,7 +144,7 @@ _initialize_ser_pipe (void) ops->next = 0; ops->open = pipe_open; ops->close = pipe_close; - ops->readchar = ser_unix_readchar; + ops->readchar = ser_base_readchar; ops->write = ser_base_write; ops->flush_output = ser_base_flush_output; ops->flush_input = ser_base_flush_input; @@ -158,5 +158,7 @@ _initialize_ser_pipe (void) ops->setstopbits = ser_base_setstopbits; ops->drain_output = ser_base_drain_output; ops->async = ser_base_async; + ops->read_prim = ser_unix_read_prim; + ops->write_prim = ser_unix_write_prim; serial_add_interface (ops); } diff --git a/gdb/ser-tcp.c b/gdb/ser-tcp.c index f908d25..e4e05af 100644 --- a/gdb/ser-tcp.c +++ b/gdb/ser-tcp.c @@ -34,11 +34,19 @@ #endif #include + +#ifdef WINAPI +#include +#define ETIMEDOUT WSAETIMEDOUT +#else #include #include #include #include #include +#define closesocket close +#define ioctlsocket ioctl +#endif #include #include "gdb_string.h" @@ -62,6 +70,11 @@ net_open (struct serial *scb, const char *name) int use_udp; struct hostent *hostent; struct sockaddr_in sockaddr; +#ifdef WINAPI + u_long ioarg; +#else + int ioarg; +#endif use_udp = 0; if (strncmp (name, "udp:", 4) == 0) @@ -108,14 +121,25 @@ net_open (struct serial *scb, const char *name) sizeof (struct in_addr)); /* set socket nonblocking */ - tmp = 1; - ioctl (scb->fd, FIONBIO, &tmp); + ioarg = 1; + ioctlsocket (scb->fd, FIONBIO, &ioarg); /* Use Non-blocking connect. connect() will return 0 if connected already. */ n = connect (scb->fd, (struct sockaddr *) &sockaddr, sizeof (sockaddr)); - if (n < 0 && errno != EINPROGRESS) + if (n < 0 +#ifdef WINAPI + /* Under Windows, calling "connect" with a non-blocking socket + results in WSAEWOULDBLOCK, not WSAEINPROGRESS. */ + && WSAGetLastError() != WSAEWOULDBLOCK +#else + && errno != EINPROGRESS +#endif + ) { +#ifdef WINAPI + errno = WSAGetLastError(); +#endif net_close (scb); return -1; } @@ -165,7 +189,11 @@ net_open (struct serial *scb, const char *name) { int res, err, len; len = sizeof(err); - res = getsockopt (scb->fd, SOL_SOCKET, SO_ERROR, &err, &len); + /* On Windows, the fourth parameter to getsockopt is a "char *"; + on UNIX systems it is generally "void *". The cast to "void *" + is OK everywhere, since in C "void *" can be implicitly + converted to any pointer type. */ + res = getsockopt (scb->fd, SOL_SOCKET, SO_ERROR, (void *) &err, &len); if (res < 0 || err) { if (err) @@ -176,8 +204,8 @@ net_open (struct serial *scb, const char *name) } /* turn off nonblocking */ - tmp = 0; - ioctl (scb->fd, FIONBIO, &tmp); + ioarg = 0; + ioctlsocket (scb->fd, FIONBIO, &ioarg); if (use_udp == 0) { @@ -202,20 +230,47 @@ net_close (struct serial *scb) if (scb->fd < 0) return; - close (scb->fd); + closesocket (scb->fd); scb->fd = -1; } +static int +net_read_prim (struct serial *scb, size_t count) +{ +#ifdef HAVE_RECV + return recv (scb->fd, scb->buf, count, 0); +#else + return ser_unix_read_prim (scb, count); +#endif +} + +static int +net_write_prim (struct serial *scb, const char *str, int len) +{ +#ifdef HAVE_SEND + return send (scb->fd, str, len, 0); +#else + return ser_unix_write_prim (scb, str, len); +#endif +} + void _initialize_ser_tcp (void) { - struct serial_ops *ops = XMALLOC (struct serial_ops); + struct serial_ops *ops; +#ifdef WINAPI + WSADATA wsa_data; + if (WSAStartup (MAKEWORD (1, 0), &wsa_data) != 0) + /* WinSock is unavailable. */ + return; +#endif + ops = XMALLOC (struct serial_ops); memset (ops, 0, sizeof (struct serial_ops)); ops->name = "tcp"; ops->next = 0; ops->open = net_open; ops->close = net_close; - ops->readchar = ser_unix_readchar; + ops->readchar = ser_base_readchar; ops->write = ser_base_write; ops->flush_output = ser_base_flush_output; ops->flush_input = ser_base_flush_input; @@ -229,5 +284,7 @@ _initialize_ser_tcp (void) ops->setstopbits = ser_base_setstopbits; ops->drain_output = ser_base_drain_output; ops->async = ser_base_async; + ops->read_prim = net_read_prim; + ops->write_prim = net_write_prim; serial_add_interface (ops); } diff --git a/gdb/ser-unix.c b/gdb/ser-unix.c index 14df459..b76a6bc 100644 --- a/gdb/ser-unix.c +++ b/gdb/ser-unix.c @@ -70,9 +70,6 @@ static void hardwire_raw (struct serial *scb); static int wait_for (struct serial *scb, int timeout); static int hardwire_readchar (struct serial *scb, int timeout); static int do_hardwire_readchar (struct serial *scb, int timeout); -static int generic_readchar (struct serial *scb, int timeout, - int (*do_readchar) (struct serial *scb, - int timeout)); static int rate_to_code (int rate); static int hardwire_setbaudrate (struct serial *scb, int rate); static void hardwire_close (struct serial *scb); @@ -422,7 +419,7 @@ hardwire_raw (struct serial *scb) */ /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent - ser_unix*() until the old TERMIOS/SGTTY/... timer code has been + ser_base*() until the old TERMIOS/SGTTY/... timer code has been flushed. . */ /* NOTE: cagney/1999-09-30: Much of the code below is dead. The only @@ -542,13 +539,13 @@ wait_for (struct serial *scb, int timeout) dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */ /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent - ser_unix*() until the old TERMIOS/SGTTY/... timer code has been + ser_base*() until the old TERMIOS/SGTTY/... timer code has been flushed. */ /* NOTE: cagney/1999-09-16: This function is not identical to - ser_unix_readchar() as part of replacing it with ser_unix*() + ser_base_readchar() as part of replacing it with ser_base*() merging will be required - this code handles the case where read() - times out due to no data while ser_unix_readchar() doesn't expect + times out due to no data while ser_base_readchar() doesn't expect that. */ static int @@ -863,191 +860,7 @@ hardwire_close (struct serial *scb) close (scb->fd); scb->fd = -1; } - -/* Wait for input on scb, with timeout seconds. Returns 0 on success, - otherwise SERIAL_TIMEOUT or SERIAL_ERROR. */ - -static int -ser_unix_wait_for (struct serial *scb, int timeout) -{ - while (1) - { - int numfds; - struct timeval tv; - fd_set readfds, exceptfds; - - /* NOTE: Some OS's can scramble the READFDS when the select() - call fails (ex the kernel with Red Hat 5.2). Initialize all - arguments before each call. */ - - tv.tv_sec = timeout; - tv.tv_usec = 0; - - FD_ZERO (&readfds); - FD_ZERO (&exceptfds); - FD_SET (scb->fd, &readfds); - FD_SET (scb->fd, &exceptfds); - - if (timeout >= 0) - numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, &tv); - else - numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, 0); - - if (numfds <= 0) - { - if (numfds == 0) - return SERIAL_TIMEOUT; - else if (errno == EINTR) - continue; - else - return SERIAL_ERROR; /* Got an error from select or poll */ - } - - return 0; - } -} - -/* Read a character with user-specified timeout. TIMEOUT is number of seconds - to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns - char if successful. Returns -2 if timeout expired, EOF if line dropped - dead, or -3 for any other error (see errno in that case). */ - -static int -do_unix_readchar (struct serial *scb, int timeout) -{ - int status; - int delta; - - /* We have to be able to keep the GUI alive here, so we break the - original timeout into steps of 1 second, running the "keep the - GUI alive" hook each time through the loop. - - Also, timeout = 0 means to poll, so we just set the delta to 0, - so we will only go through the loop once. */ - - delta = (timeout == 0 ? 0 : 1); - while (1) - { - - /* N.B. The UI may destroy our world (for instance by calling - remote_stop,) in which case we want to get out of here as - quickly as possible. It is not safe to touch scb, since - someone else might have freed it. The - deprecated_ui_loop_hook signals that we should exit by - returning 1. */ - - if (deprecated_ui_loop_hook) - { - if (deprecated_ui_loop_hook (0)) - return SERIAL_TIMEOUT; - } - - status = ser_unix_wait_for (scb, delta); - if (timeout > 0) - timeout -= delta; - - /* If we got a character or an error back from wait_for, then we can - break from the loop before the timeout is completed. */ - - if (status != SERIAL_TIMEOUT) - { - break; - } - - /* If we have exhausted the original timeout, then generate - a SERIAL_TIMEOUT, and pass it out of the loop. */ - - else if (timeout == 0) - { - status = SERIAL_TIMEOUT; - break; - } - } - - if (status < 0) - return status; - - while (1) - { - status = read (scb->fd, scb->buf, BUFSIZ); - if (status != -1 || errno != EINTR) - break; - } - - if (status <= 0) - { - if (status == 0) - return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to - distinguish between EOF & timeouts - someday] */ - else - return SERIAL_ERROR; /* Got an error from read */ - } - - scb->bufcnt = status; - scb->bufcnt--; - scb->bufp = scb->buf; - return *scb->bufp++; -} - -/* Perform operations common to both old and new readchar. */ - -/* Return the next character from the input FIFO. If the FIFO is - empty, call the SERIAL specific routine to try and read in more - characters. - - Initially data from the input FIFO is returned (fd_event() - pre-reads the input into that FIFO. Once that has been emptied, - further data is obtained by polling the input FD using the device - specific readchar() function. Note: reschedule() is called after - every read. This is because there is no guarentee that the lower - level fd_event() poll_event() code (which also calls reschedule()) - will be called. */ - -static int -generic_readchar (struct serial *scb, int timeout, - int (do_readchar) (struct serial *scb, int timeout)) -{ - int ch; - if (scb->bufcnt > 0) - { - ch = *scb->bufp; - scb->bufcnt--; - scb->bufp++; - } - else if (scb->bufcnt < 0) - { - /* Some errors/eof are are sticky. */ - ch = scb->bufcnt; - } - else - { - ch = do_readchar (scb, timeout); - if (ch < 0) - { - switch ((enum serial_rc) ch) - { - case SERIAL_EOF: - case SERIAL_ERROR: - /* Make the error/eof stick. */ - scb->bufcnt = ch; - break; - case SERIAL_TIMEOUT: - scb->bufcnt = 0; - break; - } - } - } - reschedule (scb); - return ch; -} - -int -ser_unix_readchar (struct serial *scb, int timeout) -{ - return generic_readchar (scb, timeout, do_unix_readchar); -} void _initialize_ser_hardwire (void) @@ -1058,7 +871,7 @@ _initialize_ser_hardwire (void) ops->next = 0; ops->open = hardwire_open; ops->close = hardwire_close; - /* FIXME: Don't replace this with the equivalent ser_unix*() until + /* FIXME: Don't replace this with the equivalent ser_base*() until the old TERMIOS/SGTTY/... timer code has been flushed. cagney 1999-09-16. */ ops->readchar = hardwire_readchar; @@ -1075,5 +888,29 @@ _initialize_ser_hardwire (void) ops->setstopbits = hardwire_setstopbits; ops->drain_output = hardwire_drain_output; ops->async = ser_base_async; + ops->read_prim = ser_unix_read_prim; + ops->write_prim = ser_unix_write_prim; serial_add_interface (ops); } + +int +ser_unix_read_prim (struct serial *scb, size_t count) +{ + int status; + + while (1) + { + status = read (scb->fd, scb->buf, count); + if (status != -1 || errno != EINTR) + break; + } + return status; +} + +int +ser_unix_write_prim (struct serial *scb, const void *buf, size_t len) +{ + /* ??? Historically, GDB has not retried calls to "write" that + result in EINTR. */ + return write (scb->fd, buf, len); +} diff --git a/gdb/ser-unix.h b/gdb/ser-unix.h index 5ba01f9..03dbdef 100644 --- a/gdb/ser-unix.h +++ b/gdb/ser-unix.h @@ -22,6 +22,8 @@ #ifndef SER_UNIX_H #define SER_UNIX_H -extern int ser_unix_readchar (struct serial *scb, int timeout); +extern int ser_unix_read_prim (struct serial *scb, size_t count); +extern int ser_unix_write_prim (struct serial *scb, const void *buf, + size_t count); #endif diff --git a/gdb/serial.h b/gdb/serial.h index d83b201..89b66b4 100644 --- a/gdb/serial.h +++ b/gdb/serial.h @@ -232,6 +232,12 @@ struct serial_ops the specified function when ever there is something interesting. */ void (*async) (struct serial *scb, int async_p); + /* Perform a low-level read operation, reading (at most) COUNT + bytes into SCB->BUF. */ + int (*read_prim)(struct serial *scb, size_t count); + /* Perform a low-level write operation, writing (at most) COUNT + bytes from BUF. */ + int (*write_prim)(struct serial *scb, const void *buf, size_t count); }; /* Add a new serial interface to the interface list */ diff --git a/gdb/utils.c b/gdb/utils.c index d151174..69a7702 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -58,6 +58,10 @@ #include /* For MAXPATHLEN */ +#ifdef WINAPI +#include +#endif + #include "gdb_curses.h" #include "readline/readline.h" @@ -846,14 +850,28 @@ char * safe_strerror (int errnum) { char *msg; + static char buf[32]; - msg = strerror (errnum); - if (msg == NULL) +#ifdef WINAPI + /* The strerror function only works for functions that set errno. + In the case of Windows sockets, we can get error numbers that + strerror cannot handle. */ + if (errnum > WSABASEERR) { - static char buf[32]; - xsnprintf (buf, sizeof buf, "(undocumented errno %d)", errnum); + xsnprintf (buf, sizeof buf, "(winsock error %d)", errnum); msg = buf; } + else +#endif + { + msg = strerror (errnum); + + if (msg == NULL) + { + xsnprintf (buf, sizeof buf, "(undocumented errno %d)", errnum); + msg = buf; + } + } return (msg); } -- cgit v1.1