public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Thiago Jung Bauermann <thiago.bauermann@linaro.org>
To: gdb-patches@sourceware.org
Cc: Thiago Jung Bauermann <thiago.bauermann@linaro.org>,
	Pedro Alves <pedro@palves.net>
Subject: [PATCH 4/5] gdb/linux-nat: Read auxv from any thread of the process
Date: Fri, 31 Mar 2023 03:44:31 +0000	[thread overview]
Message-ID: <20230331034432.3037148-5-thiago.bauermann@linaro.org> (raw)
In-Reply-To: <20230331034432.3037148-1-thiago.bauermann@linaro.org>

If the initial thread of the process exits, reading the process' auxiliary
vector via /proc/PID/auxv fails in one of two ways:

1. If GDB is root, then opening the file succeeds but reading from it
   returns 0 bytes.

2. If gdbserver isn't root, then opening the file fails with EACCES.

This race isn't easy to run into because one of the first things that GDB
does when starting an inferior is to read its auxiliary vector and store it
in the auxv cache.  All further queries of the auxiliary vector will be
served from there, unless one of the cache-clearing events
("inferior_exit", "inferior_appeared", "executable_changed") occurs.

To fix the race, use linux_proc_read_auxv instead of the generic procfs
implementation.

Suggested-by: Pedro Alves <pedro@palves.net>
---
 gdb/linux-nat.c | 40 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 38 insertions(+), 2 deletions(-)

diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
index fd80fd975c14..938ae1c9b8c6 100644
--- a/gdb/linux-nat.c
+++ b/gdb/linux-nat.c
@@ -3667,6 +3667,33 @@ linux_xfer_siginfo (ptid_t ptid, enum target_object object,
   return TARGET_XFER_OK;
 }
 
+/* Implement the to_xfer_partial target_ops method for TARGET_OBJECT_AUXV.  This
+   function handles access via /proc/LWP/auxv, which allows handling possible
+   races in multi-threaded inferiors.  */
+
+static enum target_xfer_status
+linux_nat_xfer_auxv (gdb_byte *readbuf, const gdb_byte *writebuf,
+		     ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
+{
+  /* Linux doesn't support writing to the auxv file.  */
+  if (readbuf == nullptr || writebuf != nullptr)
+    return TARGET_XFER_E_IO;
+
+  ssize_t xfered;
+  bool rc = linux_proc_read_auxv (inferior_ptid.pid (), readbuf, (off_t) offset,
+				  (size_t) len, xfered);
+
+  if (!rc)
+    return TARGET_XFER_E_IO;
+  else if (xfered == 0)
+    return TARGET_XFER_EOF;
+  else
+    {
+      *xfered_len = (ULONGEST) xfered;
+      return TARGET_XFER_OK;
+    }
+}
+
 static enum target_xfer_status
 linux_nat_xfer_osdata (enum target_object object,
 		       const char *annex, gdb_byte *readbuf,
@@ -3695,8 +3722,17 @@ linux_nat_target::xfer_partial (enum target_object object,
     return TARGET_XFER_EOF;
 
   if (object == TARGET_OBJECT_AUXV)
-    return memory_xfer_auxv (this, object, annex, readbuf, writebuf,
-			     offset, len, xfered_len);
+    {
+      /* For attached inferiors, use memory_xfer_auxv's ld.so support which
+	 works with virtual executables being executed by Valgrind's
+	 memcheck.  */
+      if (current_inferior ()->attach_flag)
+	return memory_xfer_auxv (this, object, annex, readbuf, writebuf,
+				 offset, len, xfered_len);
+      else
+	return linux_nat_xfer_auxv (readbuf, writebuf, offset, len,
+				    xfered_len);
+    }
 
   if (object == TARGET_OBJECT_OSDATA)
     return linux_nat_xfer_osdata (object, annex, readbuf, writebuf,

  parent reply	other threads:[~2023-03-31  3:45 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-31  3:44 [PATCH 0/5] gdbserver: Follow-up on linux_get_auxv using PID parameter Thiago Jung Bauermann
2023-03-31  3:44 ` [PATCH 1/5] gdbserver: Use current_process in handle_qxfer_auxv Thiago Jung Bauermann
2023-03-31  3:44 ` [PATCH 2/5] gdbserver: Use the PID of the current process instead of the current thread Thiago Jung Bauermann
2023-03-31  3:44 ` [PATCH 3/5] gdbserver/linux: Read auxv from any thread of the process Thiago Jung Bauermann
2023-03-31  3:44 ` Thiago Jung Bauermann [this message]
2023-03-31  3:44 ` [PATCH 5/5] gdb/testsuite: Add test that reads auxv in a multi-threaded inferior Thiago Jung Bauermann
2023-04-04 14:00   ` Alexandra Petlanova Hajkova
2023-04-04 15:19 ` [PATCH 0/5] gdbserver: Follow-up on linux_get_auxv using PID parameter Simon Marchi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230331034432.3037148-5-thiago.bauermann@linaro.org \
    --to=thiago.bauermann@linaro.org \
    --cc=gdb-patches@sourceware.org \
    --cc=pedro@palves.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).