Mirror of the gdb mailing list
 help / color / mirror / Atom feed
From: dje@google.com (Doug Evans)
To: gdb@sourceware.org
Subject: gdbserver ptrace not doing waitpid after attaching to threads?
Date: Fri, 12 Dec 2008 21:14:00 -0000	[thread overview]
Message-ID: <20081212211315.57CE01C7A79@localhost> (raw)

Hi.

There's something about gdbserver's use of ptrace that I don't understand
and I'm hoping someone can help.

AIUI, after attaching to a thread (or process) one must wait until
the thread has stopped before one can, for example, read regs.
[Well, one can cheat and assume enough time passes before trying to read
regs, but that's cheating. :-)]
If one tries to read regs too soon ptrace will return with ESRCH.

This is the behaviour I'm seeing in a version of gdbserver I'm hacking on -
my register reads are intermittently failing with ESRCH.  If I add code to
wait for SIGSTOP after attaching to all the threads then my register reads
start working.

However, gdb+gdbserver works just fine (obviously), and in fact there is
code in gdbserver to explicitly skip/ignore the initial SIGSTOP in
linux-low.c:linux_attach_lwp:

  /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
     brings it to a halt.  We should ignore that SIGSTOP and resume the process
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^
     (unless this is the first process, in which case the flag will be cleared
     in linux_attach).

     On the other hand, if we are currently trying to stop all threads, we
     should treat the new thread as if we had sent it a SIGSTOP.  This works
     because we are guaranteed that add_process added us to the end of the
     list, and so the new thread has not yet reached wait_for_sigstop (but
     will).  */
  if (! stopping_threads)
    new_process->stop_expected = 1;

and there is code linux_attach to undo this because for main thread
we *do* want to wait for the initial SIGSTOP after attaching to the process:

  /* Don't ignore the initial SIGSTOP if we just attached to this process.
     It will be collected by wait shortly.  */
  process = (struct process_info *) find_inferior_id (&all_processes, pid);
  process->stop_expected = 0;

Anyone know what's going on here?
Why the different treatment for the main thread vs the others?
Why isn't gdbserver waiting for a SIGSTOP after attaching to threads?

I've run gdbserver under strace and see that gdbserver is essentially doing
ptrace (ATTACH), ptrace (SETOPTIONS), ptrace (GETREGS) on threads
with no intervening waitpid.

I wonder if gdb is working just because of luck: maybe enough packets go back
and forth before gdb tries to read regs that all threads have stopped
by the time gdb needs them to have stopped.
In my hacked gdbserver I'm doing a register read almost immediately
after attaching.


             reply	other threads:[~2008-12-12 21:14 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-12-12 21:14 Doug Evans [this message]
2008-12-12 21:29 ` Daniel Jacobowitz
2008-12-12 23:08   ` Doug Evans
2008-12-12 23:17     ` Daniel Jacobowitz
2008-12-12 23:22       ` Doug Evans
2008-12-13  1:07         ` Daniel Jacobowitz

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=20081212211315.57CE01C7A79@localhost \
    --to=dje@google.com \
    --cc=gdb@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