public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Add common write_memory and read_memory NetBSD routines
@ 2020-10-07  4:13 Kamil Rytarowski
  2020-10-07 11:27 ` Simon Marchi
  0 siblings, 1 reply; 2+ messages in thread
From: Kamil Rytarowski @ 2020-10-07  4:13 UTC (permalink / raw)
  To: gdb-patches

Instead of sharing the native-only code with all BSDs with slightly
different semantics of the kernels, share the NetBSD-only behavior beteen
the NetBSD native and gdbserver setup.

NetBSD does not differentiate the address space I and D in the
operations (contrary to OpenBSD). NetBSD handles EACCES that integrates
with NetBSD specific PaX MPROTECT error handling.

Add a verbose message in the native client that an operation could be
cancelled due to PaX MPROTECT setup.

gdb/ChangeLog:

       * nat/netbsd-nat.c (write_memory, read_memory): Add.
       * nat/netbsd-nat.h (write_memory, read_memory): Likewise.
       * nbsd-nat.c (nbsd_nat_target::xfer_partial): Update.

gdbserver/ChangeLog:

       * netbsd-low.cc (netbsd_process_target::read_memory)
       (netbsd_process_target::write_memory): Update.
---
 gdb/ChangeLog           |  6 ++++
 gdb/nat/netbsd-nat.c    | 80 +++++++++++++++++++++++++++++++++++++++++
 gdb/nat/netbsd-nat.h    | 22 ++++++++++++
 gdb/nbsd-nat.c          | 23 ++++++++++++
 gdbserver/ChangeLog     |  5 +++
 gdbserver/netbsd-low.cc | 61 ++-----------------------------
 6 files changed, 138 insertions(+), 59 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 2744ad8e097..4d4c0cb3d90 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,9 @@
+2020-10-07  Kamil Rytarowski  <n54@gmx.com>
+
+	* nat/netbsd-nat.c (write_memory, read_memory): Add.
+	* nat/netbsd-nat.h (write_memory, read_memory): Likewise.
+	* nbsd-nat.c (nbsd_nat_target::xfer_partial): Update.
+
 2020-10-06  Andrew Burgess  <andrew.burgess@embecosm.com>

 	* symtab.c (find_pc_line): Return unmapped addresses when the
diff --git a/gdb/nat/netbsd-nat.c b/gdb/nat/netbsd-nat.c
index 41b67cb72fc..8316935f039 100644
--- a/gdb/nat/netbsd-nat.c
+++ b/gdb/nat/netbsd-nat.c
@@ -210,4 +210,84 @@ qxfer_siginfo (pid_t pid, const char *annex, unsigned char *readbuf,
   return len;
 }

+/* See netbsd-nat.h.  */
+
+int
+write_memory (pid_t pid, unsigned const char *writebuf, CORE_ADDR offset,
+	      size_t len, size_t *xfered_len)
+{
+  struct ptrace_io_desc io;
+  io.piod_op = PIOD_WRITE_D;
+  io.piod_len = len;
+
+  size_t bytes_written = 0;
+
+  /* Zero length write always succeeds.  */
+  if (len > 0)
+    {
+      do
+	{
+	  io.piod_addr = (void *)(writebuf + bytes_written);
+	  io.piod_offs = (void *)(offset + bytes_written);
+
+	  errno = 0;
+	  int rv = ptrace (PT_IO, pid, &io, 0);
+	  if (rv == -1)
+	    {
+	      gdb_assert (errno != 0);
+	      return errno;
+	    }
+	  if (io.piod_len == 0)
+	    return 0;
+
+	  bytes_written += io.piod_len;
+	  io.piod_len = len - bytes_written;
+	}
+      while (bytes_written < len);
+    }
+
+  if (xfered_len)
+    *xfered_len = bytes_written;
+
+  return 0;
+}
+
+/* See netbsd-nat.h.  */
+
+int
+read_memory (pid_t pid, unsigned char *readbuf, CORE_ADDR offset,
+	      size_t len, size_t *xfered_len)
+{
+  struct ptrace_io_desc io;
+  io.piod_op = PIOD_READ_D;
+  io.piod_len = len;
+
+  size_t bytes_read = 0;
+
+  /* Zero length write always succeeds.  */
+  if (len > 0)
+    {
+      do
+	{
+	  io.piod_offs = (void *)(offset + bytes_read);
+	  io.piod_addr = readbuf + bytes_read;
+
+	  int rv = ptrace (PT_IO, pid, &io, 0);
+	  if (rv == -1)
+	    return errno;
+	  if (io.piod_len == 0)
+	    return 0;
+
+	  bytes_read += io.piod_len;
+	  io.piod_len = len - bytes_read;
+	}
+      while (bytes_read < len);
+    }
+
+  if (xfered_len)
+    *xfered_len = bytes_read;
+
+  return 0;
+}
+
 }
diff --git a/gdb/nat/netbsd-nat.h b/gdb/nat/netbsd-nat.h
index d77119bb862..594dec1ba48 100644
--- a/gdb/nat/netbsd-nat.h
+++ b/gdb/nat/netbsd-nat.h
@@ -67,6 +67,28 @@ extern void enable_proc_events (pid_t pid);
 extern int qxfer_siginfo (pid_t pid, const char *annex, unsigned char *readbuf,
 			  unsigned const char *writebuf, CORE_ADDR offset,
 			  int len);
+
+/* Write gdb's LEN bytes from WRITEBUF and copy it to OFFSET in inferior
+   process' address space. The inferior is specified by PID.
+   Returns 0 on success or errno on failure and the number of bytes
+   on a successful transfer in XFERED_LEN.
+
+   This function assumes internally that the queried process is stopped and
+   traced.  */
+
+extern int write_memory (pid_t pid, unsigned const char *writebuf,
+			 CORE_ADDR offset, size_t len, size_t *xfered_len);
+
+/* Read inferior process's LEN bytes from OFFSET and copy it to WRITEBUF in
+   gdb's address space.
+   Returns 0 on success or errno on failure and the number of bytes
+   on a successful transfer in XFERED_LEN.
+
+   This function assumes internally that the queried process is stopped and
+   traced.  */
+
+extern int read_memory (pid_t pid, unsigned char *readbuf, CORE_ADDR offset,
+			size_t len, size_t *xfered_len);
 }

 #endif
diff --git a/gdb/nbsd-nat.c b/gdb/nbsd-nat.c
index 7a07fbf6d8a..46dcfac2cf6 100644
--- a/gdb/nbsd-nat.c
+++ b/gdb/nbsd-nat.c
@@ -764,6 +764,29 @@ nbsd_nat_target::xfer_partial (enum target_object object,
 	*xfered_len = len;
 	return TARGET_XFER_OK;
       }
+    case TARGET_OBJECT_MEMORY:
+      {
+	size_t xfered;
+	int res;
+	if (writebuf != nullptr)
+	  res = netbsd_nat::write_memory (pid, writebuf, offset, len, &xfered);
+	else
+	  res = netbsd_nat::read_memory (pid, readbuf, offset, len, &xfered);
+	if (res != 0)
+	  {
+	    if (res == EACCES)
+	      fprintf_unfiltered (gdb_stderr, "Cannot %s process at %s (%s). "
+				  "Is PaX MPROTECT active? See security(7), "
+				  "sysctl(7), paxctl(8)\n",
+				  (writebuf ? "write to" : "read from"),
+				  pulongest (offset), safe_strerror (errno));
+	    return TARGET_XFER_E_IO;
+	  }
+	if (xfered == 0)
+	  return TARGET_XFER_EOF;
+	*xfered_len = (ULONGEST) xfered;
+	return TARGET_XFER_OK;
+      }
     default:
       return inf_ptrace_target::xfer_partial (object, annex,
 					      readbuf, writebuf, offset,
diff --git a/gdbserver/ChangeLog b/gdbserver/ChangeLog
index d4e8e52c74e..305cb0f4a2b 100644
--- a/gdbserver/ChangeLog
+++ b/gdbserver/ChangeLog
@@ -1,3 +1,8 @@
+2020-10-07  Kamil Rytarowski  <n54@gmx.com>
+
+	* netbsd-low.cc (netbsd_process_target::read_memory)
+	(netbsd_process_target::write_memory): Update.
+
 2020-10-06  Shahab Vahedi  <shahab@synopsys.com>

 	* regcache.cc (register_data): Remove unused "fetch" argument.
diff --git a/gdbserver/netbsd-low.cc b/gdbserver/netbsd-low.cc
index 7bec55a56ac..e5ea8233e00 100644
--- a/gdbserver/netbsd-low.cc
+++ b/gdbserver/netbsd-low.cc
@@ -556,36 +556,8 @@ int
 netbsd_process_target::read_memory (CORE_ADDR memaddr, unsigned char *myaddr,
 				    int size)
 {
-  struct ptrace_io_desc io;
-  io.piod_op = PIOD_READ_D;
-  io.piod_len = size;
-
   pid_t pid = current_process ()->pid;
-
-  int bytes_read = 0;
-
-  if (size == 0)
-    {
-      /* Zero length write always succeeds.  */
-      return 0;
-    }
-  do
-    {
-      io.piod_offs = (void *)(memaddr + bytes_read);
-      io.piod_addr = myaddr + bytes_read;
-
-      int rv = ptrace (PT_IO, pid, &io, 0);
-      if (rv == -1)
-	return errno;
-      if (io.piod_len == 0)
-	return 0;
-
-      bytes_read += io.piod_len;
-      io.piod_len = size - bytes_read;
-    }
-  while (bytes_read < size);
-
-  return 0;
+  return netbsd_nat::read_memory (pid, myaddr, memaddr, size, nullptr);
 }

 /* Implement the write_memory target_ops method.  */
@@ -594,37 +566,8 @@ int
 netbsd_process_target::write_memory (CORE_ADDR memaddr,
 				     const unsigned char *myaddr, int size)
 {
-  struct ptrace_io_desc io;
-  io.piod_op = PIOD_WRITE_D;
-  io.piod_len = size;
-
   pid_t pid = current_process ()->pid;
-
-  int bytes_written = 0;
-
-  if (size == 0)
-    {
-      /* Zero length write always succeeds.  */
-      return 0;
-    }
-
-  do
-    {
-      io.piod_addr = (void *)(myaddr + bytes_written);
-      io.piod_offs = (void *)(memaddr + bytes_written);
-
-      int rv = ptrace (PT_IO, pid, &io, 0);
-      if (rv == -1)
-	return errno;
-      if (io.piod_len == 0)
-	return 0;
-
-      bytes_written += io.piod_len;
-      io.piod_len = size - bytes_written;
-    }
-  while (bytes_written < size);
-
-  return 0;
+  return netbsd_nat::write_memory (pid, myaddr, memaddr, size, nullptr);
 }

 /* Implement the request_interrupt target_ops method.  */
--
2.28.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] Add common write_memory and read_memory NetBSD routines
  2020-10-07  4:13 [PATCH] Add common write_memory and read_memory NetBSD routines Kamil Rytarowski
@ 2020-10-07 11:27 ` Simon Marchi
  0 siblings, 0 replies; 2+ messages in thread
From: Simon Marchi @ 2020-10-07 11:27 UTC (permalink / raw)
  To: Kamil Rytarowski, gdb-patches


Ok with these nits fixed:

On 2020-10-07 12:13 a.m., Kamil Rytarowski wrote:
> diff --git a/gdb/nat/netbsd-nat.c b/gdb/nat/netbsd-nat.c
> index 41b67cb72fc..8316935f039 100644
> --- a/gdb/nat/netbsd-nat.c
> +++ b/gdb/nat/netbsd-nat.c
> @@ -210,4 +210,84 @@ qxfer_siginfo (pid_t pid, const char *annex, unsigned char *readbuf,
>    return len;
>  }
>
> +/* See netbsd-nat.h.  */
> +
> +int
> +write_memory (pid_t pid, unsigned const char *writebuf, CORE_ADDR offset,
> +	      size_t len, size_t *xfered_len)
> +{
> +  struct ptrace_io_desc io;
> +  io.piod_op = PIOD_WRITE_D;
> +  io.piod_len = len;
> +
> +  size_t bytes_written = 0;
> +
> +  /* Zero length write always succeeds.  */
> +  if (len > 0)
> +    {
> +      do
> +	{
> +	  io.piod_addr = (void *)(writebuf + bytes_written);
> +	  io.piod_offs = (void *)(offset + bytes_written);
> +
> +	  errno = 0;
> +	  int rv = ptrace (PT_IO, pid, &io, 0);
> +	  if (rv == -1)
> +	    {
> +	      gdb_assert (errno != 0);
> +	      return errno;
> +	    }
> +	  if (io.piod_len == 0)
> +	    return 0;
> +
> +	  bytes_written += io.piod_len;
> +	  io.piod_len = len - bytes_written;
> +	}
> +      while (bytes_written < len);
> +    }
> +
> +  if (xfered_len)

!= nullptr

> +    *xfered_len = bytes_written;
> +
> +  return 0;
> +}
> +
> +/* See netbsd-nat.h.  */
> +
> +int
> +read_memory (pid_t pid, unsigned char *readbuf, CORE_ADDR offset,
> +	      size_t len, size_t *xfered_len)
> +{
> +  struct ptrace_io_desc io;
> +  io.piod_op = PIOD_READ_D;
> +  io.piod_len = len;
> +
> +  size_t bytes_read = 0;
> +
> +  /* Zero length write always succeeds.  */
> +  if (len > 0)
> +    {
> +      do
> +	{
> +	  io.piod_offs = (void *)(offset + bytes_read);
> +	  io.piod_addr = readbuf + bytes_read;
> +
> +	  int rv = ptrace (PT_IO, pid, &io, 0);
> +	  if (rv == -1)
> +	    return errno;
> +	  if (io.piod_len == 0)
> +	    return 0;
> +
> +	  bytes_read += io.piod_len;
> +	  io.piod_len = len - bytes_read;
> +	}
> +      while (bytes_read < len);
> +    }
> +
> +  if (xfered_len)

!= nullptr

> diff --git a/gdbserver/netbsd-low.cc b/gdbserver/netbsd-low.cc
> index 7bec55a56ac..e5ea8233e00 100644
> --- a/gdbserver/netbsd-low.cc
> +++ b/gdbserver/netbsd-low.cc
> @@ -556,36 +556,8 @@ int
>  netbsd_process_target::read_memory (CORE_ADDR memaddr, unsigned char *myaddr,
>  				    int size)
>  {
> -  struct ptrace_io_desc io;
> -  io.piod_op = PIOD_READ_D;
> -  io.piod_len = size;
> -
>    pid_t pid = current_process ()->pid;
> -
> -  int bytes_read = 0;
> -
> -  if (size == 0)
> -    {
> -      /* Zero length write always succeeds.  */

write -> read

Simon

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-10-07 11:27 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-07  4:13 [PATCH] Add common write_memory and read_memory NetBSD routines Kamil Rytarowski
2020-10-07 11:27 ` Simon Marchi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).