This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Add native error to file transfer protocol error packet


Hi,

Following up on:
http://sourceware.org/ml/gdb/2008-01/msg00360.html

Here's the patch to implement it:

It goes on top of:
http://sourceware.org/ml/gdb-patches/2008-01/msg00859.html

--
Pedro Alves

gdb/
2008-01-29  Pedro Alves  <pedro_alves@portugalmail.pt>

	* remote.c(remote_error): New struct.
	(remote_error_clear): New function.
	(remote_buffer_add_int): Update doc.
	(remote_hostio_parse_result): Update doc.  Take a pointer to
	remote_error instead of a pointer to int.  Clear remote_error.
	Parse a second error number and store it in the native field of
	remote_error.
	(remote_hostio_send_command, remote_hostio_open)
	(remote_hostio_pwrite, remote_hostio_pread, remote_hostio_close)
	(remote_hostio_unlink): Update docs.  Take a pointer to
	remote_error instead of a pointer to int.  Update usages of the
	old int pointer.
	(remote_fileio_errno_to_host): Delete.
	(remote_fileio_errno_to_string): New.
	(remote_hostio_error): Update error messages to include the native
	error.
	(remote_hostio_close_cleanup, remote_file_put, remote_file_get)
	(remote_file_delete): Use a remote_error, not an int to store the
	remote error.  Likewise.  Update usages of old int.

gdbserver/
2008-01-29  Pedro Alves  <pedro_alves@portugalmail.pt>

	* hostio-errno.c (hostio_last_error_from_errno): Also output
	 native error.  gdb/gdbserver/win32-low.c
	 (wince_hostio_last_error) [_WIN32_WCE]: Likewise.

---
 gdb/gdbserver/hostio-errno.c |    2 
 gdb/gdbserver/win32-low.c    |    2 
 gdb/remote.c                 |  151 +++++++++++++++++++++++++++----------------
 3 files changed, 98 insertions(+), 57 deletions(-)

Index: src/gdb/remote.c
===================================================================
--- src.orig/gdb/remote.c	2008-01-31 18:09:52.000000000 +0000
+++ src/gdb/remote.c	2008-01-31 18:11:22.000000000 +0000
@@ -6578,6 +6578,21 @@ remote_read_description (struct target_o
 /* Remote file transfer support.  This is host-initiated I/O, not
    target-initiated; for target-initiated, see remote-fileio.c.  */
 
+struct remote_error
+{
+  int hostio; /* hostio errors defined in src/include/gdb/fileio.h */
+  ULONGEST native; /* Untranslated error as seen on the target.  */
+  int have_native; /* True if target sent native error.  */
+};
+
+static void
+remote_error_clear (struct remote_error *remote_error)
+{
+  remote_error->hostio = 0;
+  remote_error->native = 0;
+  remote_error->have_native = 0;
+}
+
 /* If *LEFT is at least the length of STRING, copy STRING to
    *BUFFER, update *BUFFER to point to the new end of the buffer, and
    decrease *LEFT.  Otherwise raise an error.  */
@@ -6644,7 +6659,7 @@ remote_buffer_add_int (char **buffer, in
 }
 
 /* Parse an I/O result packet from BUFFER.  Set RETCODE to the return
-   value, *REMOTE_ERRNO to the remote error number or zero if none
+   value, *REMOTE_ERROR to the remote error or clear it if none
    was included, and *ATTACHMENT to point to the start of the annex
    if any.  The length of the packet isn't needed here; there may
    be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
@@ -6654,11 +6669,12 @@ remote_buffer_add_int (char **buffer, in
 
 static int
 remote_hostio_parse_result (char *buffer, int *retcode,
-			    int *remote_errno, char **attachment)
+			    struct remote_error *remote_error,
+			    char **attachment)
 {
   char *p, *p2;
 
-  *remote_errno = 0;
+  remote_error_clear (remote_error);
   *attachment = NULL;
 
   if (buffer[0] != 'F')
@@ -6669,16 +6685,23 @@ remote_hostio_parse_result (char *buffer
   if (errno != 0 || p == &buffer[1])
     return -1;
 
-  /* Check for ",errno".  */
+  /* Check for ",hostio".  */
   if (*p == ',')
     {
       errno = 0;
-      *remote_errno = strtol (p + 1, &p2, 16);
+      remote_error->hostio = strtol (p + 1, &p2, 16);
       if (errno != 0 || p + 1 == p2)
 	return -1;
       p = p2;
     }
 
+  /* Check for ",native".  */
+  if (*p == ',')
+    {
+      p2 = unpack_varlen_hex (p + 1, &remote_error->native);
+      remote_error->have_native = 1;
+    }
+
   /* Check for ";attachment".  If there is no attachment, the
      packet should end here.  */
   if (*p == ';')
@@ -6698,8 +6721,8 @@ remote_hostio_parse_result (char *buffer
 
    COMMAND_BYTES is the length of the request to send, which may include
    binary data.  WHICH_PACKET is the packet configuration to check
-   before attempting a packet.  If an error occurs, *REMOTE_ERRNO
-   is set to the error number and -1 is returned.  Otherwise the value
+   before attempting a packet.  If an error occurs, *REMOTE_ERROR
+   is filled with the error number and -1 is returned.  Otherwise the value
    returned by the function is returned.
 
    ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
@@ -6710,16 +6733,17 @@ remote_hostio_parse_result (char *buffer
 
 static int
 remote_hostio_send_command (int command_bytes, int which_packet,
-			    int *remote_errno, char **attachment,
-			    int *attachment_len)
+			    struct remote_error *remote_error,
+			    char **attachment, int *attachment_len)
 {
   struct remote_state *rs = get_remote_state ();
   int ret, bytes_read;
   char *attachment_tmp;
+  remote_error_clear (remote_error);
 
   if (remote_protocol_packets[which_packet].support == PACKET_DISABLE)
     {
-      *remote_errno = FILEIO_ENOSYS;
+      remote_error->hostio = FILEIO_ENOSYS;
       return -1;
     }
 
@@ -6730,26 +6754,26 @@ remote_hostio_send_command (int command_
      buffer.  */
   if (bytes_read < 0)
     {
-      *remote_errno = FILEIO_EINVAL;
+      remote_error->hostio = FILEIO_EINVAL;
       return -1;
     }
 
   switch (packet_ok (rs->buf, &remote_protocol_packets[which_packet]))
     {
     case PACKET_ERROR:
-      *remote_errno = FILEIO_EINVAL;
+      remote_error->hostio = FILEIO_EINVAL;
       return -1;
     case PACKET_UNKNOWN:
-      *remote_errno = FILEIO_ENOSYS;
+      remote_error->hostio = FILEIO_ENOSYS;
       return -1;
     case PACKET_OK:
       break;
     }
 
-  if (remote_hostio_parse_result (rs->buf, &ret, remote_errno,
+  if (remote_hostio_parse_result (rs->buf, &ret, remote_error,
 				  &attachment_tmp))
     {
-      *remote_errno = FILEIO_EINVAL;
+      remote_error->hostio = FILEIO_EINVAL;
       return -1;
     }
 
@@ -6757,7 +6781,7 @@ remote_hostio_send_command (int command_
   if ((attachment_tmp == NULL && attachment != NULL)
       || (attachment_tmp != NULL && attachment == NULL))
     {
-      *remote_errno = FILEIO_EINVAL;
+      remote_error->hostio = FILEIO_EINVAL;
       return -1;
     }
 
@@ -6774,11 +6798,11 @@ remote_hostio_send_command (int command_
 
 /* Open FILENAME on the remote target, using FLAGS and MODE.  Return a
    remote file descriptor, or -1 if an error occurs (and set
-   *REMOTE_ERRNO).  */
+   *REMOTE_ERROR).  */
 
 static int
 remote_hostio_open (const char *filename, int flags, int mode,
-		    int *remote_errno)
+		    struct remote_error *remote_error)
 {
   struct remote_state *rs = get_remote_state ();
   char *p = rs->buf;
@@ -6796,16 +6820,16 @@ remote_hostio_open (const char *filename
   remote_buffer_add_int (&p, &left, mode);
 
   return remote_hostio_send_command (p - rs->buf, PACKET_vFile_open,
-				     remote_errno, NULL, NULL);
+				     remote_error, NULL, NULL);
 }
 
 /* Write up to LEN bytes from WRITE_BUF to FD on the remote target.
    Return the number of bytes written, or -1 if an error occurs (and
-   set *REMOTE_ERRNO).  */
+   set *REMOTE_ERROR).  */
 
 static int
 remote_hostio_pwrite (int fd, const gdb_byte *write_buf, int len,
-		      ULONGEST offset, int *remote_errno)
+		      ULONGEST offset, struct remote_error *remote_error)
 {
   struct remote_state *rs = get_remote_state ();
   char *p = rs->buf;
@@ -6824,16 +6848,16 @@ remote_hostio_pwrite (int fd, const gdb_
 			     get_remote_packet_size () - (p - rs->buf));
 
   return remote_hostio_send_command (p - rs->buf, PACKET_vFile_pwrite,
-				     remote_errno, NULL, NULL);
+				     remote_error, NULL, NULL);
 }
 
 /* Read up to LEN bytes FD on the remote target into READ_BUF
    Return the number of bytes read, or -1 if an error occurs (and
-   set *REMOTE_ERRNO).  */
+   set *REMOTE_ERROR).  */
 
 static int
 remote_hostio_pread (int fd, gdb_byte *read_buf, int len,
-		     ULONGEST offset, int *remote_errno)
+		     ULONGEST offset, struct remote_error *remote_error)
 {
   struct remote_state *rs = get_remote_state ();
   char *p = rs->buf;
@@ -6853,7 +6877,7 @@ remote_hostio_pread (int fd, gdb_byte *r
   remote_buffer_add_int (&p, &left, offset);
 
   ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_pread,
-				    remote_errno, &attachment,
+				    remote_error, &attachment,
 				    &attachment_len);
 
   if (ret < 0)
@@ -6868,10 +6892,10 @@ remote_hostio_pread (int fd, gdb_byte *r
 }
 
 /* Close FD on the remote target.  Return 0, or -1 if an error occurs
-   (and set *REMOTE_ERRNO).  */
+   (and set *REMOTE_ERROR).  */
 
 static int
-remote_hostio_close (int fd, int *remote_errno)
+remote_hostio_close (int fd, struct remote_error *remote_error)
 {
   struct remote_state *rs = get_remote_state ();
   char *p = rs->buf;
@@ -6882,14 +6906,14 @@ remote_hostio_close (int fd, int *remote
   remote_buffer_add_int (&p, &left, fd);
 
   return remote_hostio_send_command (p - rs->buf, PACKET_vFile_close,
-				     remote_errno, NULL, NULL);
+				     remote_error, NULL, NULL);
 }
 
 /* Unlink FILENAME on the remote target.  Return 0, or -1 if an error
-   occurs (and set *REMOTE_ERRNO).  */
+   occurs (and set *REMOTE_ERROR).  */
 
 static int
-remote_hostio_unlink (const char *filename, int *remote_errno)
+remote_hostio_unlink (const char *filename, struct remote_error *remote_error)
 {
   struct remote_state *rs = get_remote_state ();
   char *p = rs->buf;
@@ -6901,7 +6925,7 @@ remote_hostio_unlink (const char *filena
 			   strlen (filename));
 
   return remote_hostio_send_command (p - rs->buf, PACKET_vFile_unlink,
-				     remote_errno, NULL, NULL);
+				     remote_error, NULL, NULL);
 }
 
 static int
@@ -6956,14 +6980,28 @@ remote_fileio_errno_to_host (int errnum)
 }
 
 static char *
-remote_hostio_error (int errnum)
+remote_hostio_error (struct remote_error *remote_error)
 {
-  int host_error = remote_fileio_errno_to_host (errnum);
+  int host_error = remote_fileio_errno_to_host (remote_error->hostio);
 
   if (host_error == -1)
-    error (_("Unknown remote I/O error %d"), errnum);
+    {
+      if (remote_error->have_native)
+	error (_("Unkown remote I/O error %d.  Target native error was: %s"),
+	       remote_error->hostio,
+	       int_string (remote_error->native, 16, 0, 0, 1));
+      else
+	error (_("Unkown remote I/O error."));
+    }
   else
-    error (_("Remote I/O error: %s"), safe_strerror (host_error));
+    {
+      if (remote_error->have_native)
+	error (_("Remote I/O error: %s.  Target native error was: %s"),
+	       safe_strerror (host_error),
+	       int_string (remote_error->native, 16, 0, 0, 1));
+      else
+	error (_("Remote I/O error: %s."), safe_strerror (host_error));
+    }
 }
 
 static void
@@ -6976,16 +7014,17 @@ static void
 remote_hostio_close_cleanup (void *opaque)
 {
   int fd = *(int *) opaque;
-  int remote_errno;
+  struct remote_error remote_error;
 
-  remote_hostio_close (fd, &remote_errno);
+  remote_hostio_close (fd, &remote_error);
 }
 
 void
 remote_file_put (const char *local_file, const char *remote_file, int from_tty)
 {
   struct cleanup *back_to, *close_cleanup;
-  int retcode, fd, remote_errno, bytes, io_size;
+  int retcode, fd, bytes, io_size;
+  struct remote_error remote_error;
   FILE *file;
   gdb_byte *buffer;
   int bytes_in_buffer;
@@ -7001,10 +7040,9 @@ remote_file_put (const char *local_file,
   back_to = make_cleanup (fclose_cleanup, file);
 
   fd = remote_hostio_open (remote_file, (FILEIO_O_WRONLY | FILEIO_O_CREAT
-					 | FILEIO_O_TRUNC),
-			   0700, &remote_errno);
+					 | FILEIO_O_TRUNC), 0700, &remote_error);
   if (fd == -1)
-    remote_hostio_error (remote_errno);
+    remote_hostio_error (&remote_error);
 
   /* Send up to this many bytes at once.  They won't all fit in the
      remote packet limit, so we'll transfer slightly fewer.  */
@@ -7043,10 +7081,10 @@ remote_file_put (const char *local_file,
       bytes += bytes_in_buffer;
       bytes_in_buffer = 0;
 
-      retcode = remote_hostio_pwrite (fd, buffer, bytes, offset, &remote_errno);
+      retcode = remote_hostio_pwrite (fd, buffer, bytes, offset, &remote_error);
 
       if (retcode < 0)
-	remote_hostio_error (remote_errno);
+	remote_hostio_error (&remote_error);
       else if (retcode == 0)
 	error (_("Remote write of %d bytes returned 0!"), bytes);
       else if (retcode < bytes)
@@ -7061,8 +7099,8 @@ remote_file_put (const char *local_file,
     }
 
   discard_cleanups (close_cleanup);
-  if (remote_hostio_close (fd, &remote_errno))
-    remote_hostio_error (remote_errno);
+  if (remote_hostio_close (fd, &remote_error))
+    remote_hostio_error (&remote_error);
 
   if (from_tty)
     printf_filtered (_("Successfully sent file \"%s\".\n"), local_file);
@@ -7073,7 +7111,8 @@ void
 remote_file_get (const char *remote_file, const char *local_file, int from_tty)
 {
   struct cleanup *back_to, *close_cleanup;
-  int retcode, fd, remote_errno, bytes, io_size;
+  int retcode, fd, bytes, io_size;
+  struct remote_error remote_error;
   FILE *file;
   gdb_byte *buffer;
   ULONGEST offset;
@@ -7081,9 +7120,10 @@ remote_file_get (const char *remote_file
   if (!remote_desc)
     error (_("command can only be used with remote target"));
 
-  fd = remote_hostio_open (remote_file, FILEIO_O_RDONLY, 0, &remote_errno);
+  fd = remote_hostio_open (remote_file, FILEIO_O_RDONLY, 0,
+			   &remote_error);
   if (fd == -1)
-    remote_hostio_error (remote_errno);
+    remote_hostio_error (&remote_error);
 
   file = fopen (local_file, "wb");
   if (file == NULL)
@@ -7101,12 +7141,12 @@ remote_file_get (const char *remote_file
   offset = 0;
   while (1)
     {
-      bytes = remote_hostio_pread (fd, buffer, io_size, offset, &remote_errno);
+      bytes = remote_hostio_pread (fd, buffer, io_size, offset, &remote_error);
       if (bytes == 0)
 	/* Success, but no bytes, means end-of-file.  */
 	break;
       if (bytes == -1)
-	remote_hostio_error (remote_errno);
+	remote_hostio_error (&remote_error);
 
       offset += bytes;
 
@@ -7116,8 +7156,8 @@ remote_file_get (const char *remote_file
     }
 
   discard_cleanups (close_cleanup);
-  if (remote_hostio_close (fd, &remote_errno))
-    remote_hostio_error (remote_errno);
+  if (remote_hostio_close (fd, &remote_error))
+    remote_hostio_error (&remote_error);
 
   if (from_tty)
     printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file);
@@ -7127,14 +7167,15 @@ remote_file_get (const char *remote_file
 void
 remote_file_delete (const char *remote_file, int from_tty)
 {
-  int retcode, remote_errno;
+  int retcode;
+  struct remote_error remote_error;
 
   if (!remote_desc)
     error (_("command can only be used with remote target"));
 
-  retcode = remote_hostio_unlink (remote_file, &remote_errno);
+  retcode = remote_hostio_unlink (remote_file, &remote_error);
   if (retcode == -1)
-    remote_hostio_error (remote_errno);
+    remote_hostio_error (&remote_error);
 
   if (from_tty)
     printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file);
Index: src/gdb/gdbserver/hostio-errno.c
===================================================================
--- src.orig/gdb/gdbserver/hostio-errno.c	2008-01-31 18:09:52.000000000 +0000
+++ src/gdb/gdbserver/hostio-errno.c	2008-01-31 18:11:22.000000000 +0000
@@ -59,6 +59,6 @@ hostio_last_error_from_errno (char *buf)
 {
   int error = errno;
   int fileio_error = errno_to_fileio_error (error);
-  sprintf (buf, "F-1,%x", fileio_error);
+  sprintf (buf, "F-1,%x,%x", fileio_error, error);
   errno = error; /* preserve errno */
 }
Index: src/gdb/gdbserver/win32-low.c
===================================================================
--- src.orig/gdb/gdbserver/win32-low.c	2008-01-31 18:09:52.000000000 +0000
+++ src/gdb/gdbserver/win32-low.c	2008-01-31 18:11:22.000000000 +0000
@@ -1709,7 +1709,7 @@ wince_hostio_last_error (char *buf)
 {
   DWORD winerr = GetLastError ();
   int fileio_err = win32_error_to_fileio_error (winerr);
-  sprintf (buf, "F-1,%x", fileio_err);
+  sprintf (buf, "F-1,%x,%x", fileio_err, (int) winerr);
   SetLastError (winerr); /* preserve last error */
 }
 #endif

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]