public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] linux_nat_target::xfer_partial: Fallback to ptrace
@ 2022-05-12 18:15 Keith Seitz
  2022-05-20 18:51 ` Pedro Alves
  0 siblings, 1 reply; 9+ messages in thread
From: Keith Seitz @ 2022-05-12 18:15 UTC (permalink / raw)
  To: gdb-patches

Commit 05c06f318fd9a112529dfc313e6512b399a645e4 enabled GDB
to access memory while threads are running. It did this by accessing
/proc/PID/task/LWP/mem.

Unfortunatley, this interface is not implemented for writing in older kernels
(such as RHEL6). This means that GDB is unable to insert breakpoints on
these hosts:

$ ./gdb -q gdb -ex start
Reading symbols from gdb...
Temporary breakpoint 1 at 0x40fdd5: file ../../src/gdb/gdb.c, line 28.
Starting program: /home/rhel6/fsf/linux/gdb/gdb
Warning:
Cannot insert breakpoint 1.
Cannot access memory at address 0x40fdd5

(gdb)

Before this patch, linux_proc_xfer_memory_partial (previously called
linux_proc_xfer_partial) would return TARGET_XFER_EOF if the write
to /proc/PID/mem failed. [More specifically, linux_proc_xfer_partial would
not "bother for one word," but the effect is the essentially same.]

This status was checked by linux_nat_target::xfer_partial, which would then
fallback to using ptrace to perform the operation.

This is the specific hunk that removed the fallback:

-  xfer = linux_proc_xfer_partial (object, annex, readbuf, writebuf,
-                                 offset, len, xfered_len);
-  if (xfer != TARGET_XFER_EOF)
-    return xfer;
+      return linux_proc_xfer_memory_partial (readbuf, writebuf,
+                                            offset, len, xfered_len);
+    }

   return inf_ptrace_target::xfer_partial (object, annex, readbuf, writebuf,
                                          offset, len, xfered_len);

This patch restores this fallback mechanism, enabling GDB to insert
breakpoints on these older kernels.

Tested on {unix,native-gdbserver,native-extended-gdbserver}/-m{32,64}
on x86_64, s390x, aarch64, and ppc64le.
---
 gdb/linux-nat.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
index 740cc0ddfc0..f5490c27bf5 100644
--- a/gdb/linux-nat.c
+++ b/gdb/linux-nat.c
@@ -3706,8 +3706,12 @@ linux_nat_target::xfer_partial (enum target_object object,
       if (addr_bit < (sizeof (ULONGEST) * HOST_CHAR_BIT))
 	offset &= ((ULONGEST) 1 << addr_bit) - 1;
 
-      return linux_proc_xfer_memory_partial (readbuf, writebuf,
-					     offset, len, xfered_len);
+      enum target_xfer_status xfer
+	= linux_proc_xfer_memory_partial (readbuf, writebuf,
+					  offset, len, xfered_len);
+      if (xfer != TARGET_XFER_EOF)
+	return xfer;
+      /* Fallthrough to ptrace.  */
     }
 
   return inf_ptrace_target::xfer_partial (object, annex, readbuf, writebuf,
-- 
2.36.1


^ permalink raw reply	[flat|nested] 9+ messages in thread
* Re: [PATCH] linux_nat_target::xfer_partial: Fallback to ptrace
@ 2022-06-03 15:18 Keith Seitz
  2022-07-21 15:03 ` Keith Seitz
  0 siblings, 1 reply; 9+ messages in thread
From: Keith Seitz @ 2022-06-03 15:18 UTC (permalink / raw)
  To: gdb-patches; +Cc: pedro

Pedro Alves wrote:

> I guess the write to /proc/pid/mem fails with EIO for you, and there's nothing
> else we can use to detect the scenario.  So we probably want to check TARGET_XFER_E_IO
> instead.  And, maybe only do the fallback if writing.

I've updated the patch to include these changes and retested on all the "usual"
target boards on Fedora 36 x86_64, s390x, ppc64le, and aarch64 and on RHEL6
x86_64.

Keith

--

Commit 05c06f318fd9a112529dfc313e6512b399a645e4 enabled GDB
to access memory while threads are running. It did this by accessing
/proc/PID/task/LWP/mem.

Unfortunately, this interface is not implemented for writing in older kernels
(such as RHEL6). This means that GDB is unable to insert breakpoints on
these hosts:

$ ./gdb -q gdb -ex start
Reading symbols from gdb...
Temporary breakpoint 1 at 0x40fdd5: file ../../src/gdb/gdb.c, line 28.
Starting program: /home/rhel6/fsf/linux/gdb/gdb
Warning:
Cannot insert breakpoint 1.
Cannot access memory at address 0x40fdd5

(gdb)

Before this patch, linux_proc_xfer_memory_partial (previously called
linux_proc_xfer_partial) would return TARGET_XFER_EOF if the write
to /proc/PID/mem failed. [More specifically, linux_proc_xfer_partial would
not "bother for one word," but the effect is the essentially same.]

This status was checked by linux_nat_target::xfer_partial, which would then
fallback to using ptrace to perform the operation.

This is the specific hunk that removed the fallback:

-  xfer = linux_proc_xfer_partial (object, annex, readbuf, writebuf,
-                                 offset, len, xfered_len);
-  if (xfer != TARGET_XFER_EOF)
-    return xfer;
+      return linux_proc_xfer_memory_partial (readbuf, writebuf,
+                                            offset, len, xfered_len);
+    }

   return inf_ptrace_target::xfer_partial (object, annex, readbuf, writebuf,
                                          offset, len, xfered_len);

This patch restores this fallback mechanism, enabling GDB to insert
breakpoints on these older kernels. Note that a recent patch changed
the return status from TARGET_XFER_EOF to TARGET_XFER_E_IO.

Tested on {unix,native-gdbserver,native-extended-gdbserver}/-m{32,64}
on x86_64, s390x, aarch64, and ppc64le.
---
 gdb/linux-nat.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
index 3b5400896bc..571c97137c2 100644
--- a/gdb/linux-nat.c
+++ b/gdb/linux-nat.c
@@ -3706,8 +3706,12 @@ linux_nat_target::xfer_partial (enum target_object object,
       if (addr_bit < (sizeof (ULONGEST) * HOST_CHAR_BIT))
 	offset &= ((ULONGEST) 1 << addr_bit) - 1;
 
-      return linux_proc_xfer_memory_partial (readbuf, writebuf,
-					     offset, len, xfered_len);
+      enum target_xfer_status xfer
+	= linux_proc_xfer_memory_partial (readbuf, writebuf,
+					  offset, len, xfered_len);
+      if (xfer != TARGET_XFER_E_IO || readbuf != nullptr)
+	return xfer;
+      /* Fallthrough to ptrace.  /proc/pid/mem wasn't writable before Linux 2.6.39.  */
     }
 
   return inf_ptrace_target::xfer_partial (object, annex, readbuf, writebuf,
-- 
2.36.1


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

end of thread, other threads:[~2022-07-26 19:16 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-12 18:15 [PATCH] linux_nat_target::xfer_partial: Fallback to ptrace Keith Seitz
2022-05-20 18:51 ` Pedro Alves
2022-05-24 18:56   ` Keith Seitz
2022-05-25 13:41     ` Pedro Alves
2022-06-03 15:18 Keith Seitz
2022-07-21 15:03 ` Keith Seitz
2022-07-21 20:07   ` Pedro Alves
2022-07-26 17:24     ` Keith Seitz
2022-07-26 19:16       ` Pedro Alves

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).