public inbox for gdb-prs@sourceware.org
help / color / mirror / Atom feed
From: "cvs-commit at gcc dot gnu.org" <sourceware-bugzilla@sourceware.org>
To: gdb-prs@sourceware.org
Subject: [Bug threads/31579] Cannot find user-level thread for LWP
Date: Fri, 26 Apr 2024 21:02:55 +0000	[thread overview]
Message-ID: <bug-31579-4717-Wii4wTIKQk@http.sourceware.org/bugzilla/> (raw)
In-Reply-To: <bug-31579-4717@http.sourceware.org/bugzilla/>

https://sourceware.org/bugzilla/show_bug.cgi?id=31579

--- Comment #4 from Sourceware Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Pedro Alves <palves@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=5e86aab85118ae705bbf9b247497167a8d801d2c

commit 5e86aab85118ae705bbf9b247497167a8d801d2c
Author: Pedro Alves <pedro@palves.net>
Date:   Wed Apr 10 20:00:26 2024 +0100

    gdb/linux-nat: Fix mem access ptrace fallback (PR threads/31579)

    Old RHEL systems have a kernel that does not support writing memory
    via /proc/pid/mem.  On such systems, we fallback to accessing memory
    via ptrace.  That has a few downsides described in the "Accessing
    inferior memory" section at the top of linux-nat.c.

    The target_xfer interface for memory access uses inferior_ptid as
    sideband argument to indicate which process to access.  Memory access
    is process-wide, it is not thread-specific, so inferior_ptid is
    sometimes pointed at a process-wide ptid_t for the memory access
    (i.e., a ptid that looks like {pid, 0, 0}).  That is the case for any
    code that uses scoped_restore_current_inferior_for_memory, for
    example.

    That is what causes the issue described in PR 31579, where thread_db
    calls into the debugger to read memory, which reaches our
    ps_xfer_memory function, which does:

      static ps_err_e
      ps_xfer_memory (const struct ps_prochandle *ph, psaddr_t addr,
                      gdb_byte *buf, size_t len, int write)
      {
        scoped_restore_current_inferior_for_memory save_inferior
(ph->thread->inf);

        ...
          ret = target_read_memory (core_addr, buf, len);
        ...
      }

    If linux_nat_target::xfer_partial falls back to inf_ptrace_target with
    a pid-ptid, then the ptrace code will do the ptrace call targeting
    pid, the leader LWP.  That may fail with ESRCH if the leader is
    currently running, or zombie.  That is the case in the scenario in
    question, because thread_db is consulted for an event of a non-leader
    thread, before we've stopped the whole process.

    Fix this by having the ptrace fallback code try to find a stopped LWP
    to use with ptrace.

    I chose to handle this in the linux-nat target instead of in common
    code because (global) memory is a process-wide property, and this
    avoids having to teach all the code paths that use
    scoped_restore_current_inferior_for_memory to find some stopped thread
    to access memory through, which is a ptrace quirk.  That is
    effectively what we used to do before we started relying on writable
    /proc/pid/mem.  I'd rather not go back there.

    To trigger this on modern kernels you have to hack linux-nat.c to
    force the ptrace fallback code, like so:

     --- a/gdb/linux-nat.c
     +++ b/gdb/linux-nat.c
     @@ -3921,7 +3921,7 @@ linux_nat_target::xfer_partial (enum target_object
object,
              poke would incorrectly write memory to the post-exec address
              space, while the core was trying to write to the pre-exec
              address space.  */
     -      if (proc_mem_file_is_writable ())
     +      if (0 && proc_mem_file_is_writable ())

    With that hack, I was able to confirm that the fix fixes hundreds of
    testsuite failures.  Compared to a test run with pristine master, the
    hack above + this commit's fix shows that some non-stop-related tests
    fail, but that is expected, because those are tests that need to
    access memory while the program is running.  (I made no effort to
    temporarily pause an lwp if no ptrace-stopped lwp is found.)

    Change-Id: I24a4f558e248aff7bc7c514a88c698f379f23180
    Tested-By: Hannes Domani <ssbssa@yahoo.de>
    Approved-By: Andrew Burgess <aburgess@redhat.com>

-- 
You are receiving this mail because:
You are on the CC list for the bug.

  parent reply	other threads:[~2024-04-26 21:02 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-29 20:11 [Bug threads/31579] New: " ssbssa at sourceware dot org
2024-03-29 20:15 ` [Bug threads/31579] " ssbssa at sourceware dot org
2024-03-31 12:22 ` ssbssa at sourceware dot org
2024-04-11 11:41 ` pedro at palves dot net
2024-04-11 12:00 ` pedro at palves dot net
2024-04-26 21:02 ` cvs-commit at gcc dot gnu.org [this message]
2024-05-03 16:27 ` pedro at palves dot net
2024-05-03 16:28 ` pedro at palves dot net

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=bug-31579-4717-Wii4wTIKQk@http.sourceware.org/bugzilla/ \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=gdb-prs@sourceware.org \
    /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).